02-15-2011 01:00 AM
Dear all,
In my program i am using command button (named SCAN) to scan parameter on the serial port. When i press SCAN button callback fuction is starting to send command on serial for scan parameter on serial port. There are 99 command to be send and read. It is taking around 3 minutes. It is working fine. Now i want to stop scanning and come out from callback function called by SCAN button. Presently When i press SCAN button it will complete all 99 commend and then control come to UI. So i am not able to stop and come out from that loop. Is there any why to exit such loop if user want to come out?otherwise it will complete scan.
--Vishnu
Solved! Go to Solution.
02-15-2011 01:14 AM
Hi ,
I'll give you 3 options:
1. If you want to go out programaticly you can use simple Break when stop "event" occur.
2. If you want to go out using an abort button you can use this post :
http://forums.ni.com/t5/LabWindows-CVI/few-General-CVI-Questions-How-the-compiler-works/m-p/1440984
where I already asked that , this is not the recommended option for panels.
3. The best option is to use multi threading each button in separated thread....
02-15-2011 03:26 AM
Dear Kobi Kalif,
Thanks for reply. I understood solution 2 & 3. But still don't understand option-1
1. If you want to go out programaticly you can use simple Break when stop "event" occur.
Actually When i press SCAN button and program is in while loop, i can't press stop button, even UI is like disable. So how i can give stop "event"?
--Vishnu
02-15-2011 03:42 AM - edited 02-15-2011 03:42 AM
Vishnu,
while you are executing a callback the system is tied to that function and does not handle other UI events like pressing a button. The thread linked in Kibi Kalif answer explains how to fix this situation.
02-15-2011 03:46 AM
In option 1 :
lets say this is your loop :
for(.....)
{
command 1.........
command 2..........
............
if (stopVar == 1)
break;
}
this option is good if you have a var that you can use when you know an error occur or you need to finish the loop..
02-15-2011 04:29 AM
Thanks both of you. Now i am clear.
--Vishnu
02-15-2011 05:33 AM
main() { static int SerialFlag; LoadPanel(myPanel,...); InstallMainCallback (myPanel, &SerialFlag, 1); InstallCtrlCallback (myPanel, STARTBUTTON, StartButtonCallback, &SerialFlag); InstallCtrlCallback (myPanel, STOPBUTTON, StopButtonCallback, &SerialFlag); } int CVICALLBACK MainCallbackFunction (int panelOrMenuBarHandle, int controlOrMenuItemID, int event, void *callbackData, int eventData1, int eventData2) { static int TestCounter; switch(event) { case EVENT_IDLE // NI diapproves of this if(*(int *)callbackData)) // SerialFlag { SerialFunction(TestCounter); // Do each part step by step if(TestCounter<99) TestCounter++; else TestCounter=0; } } return 0; } int SerialFunction(int tcount) { int s_err; // s_err=SendMessage(tcount); return s_err; } int CVICALLBACK StartButtonCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: *(int *)callbackData=1; // Set SerialFlag break; } return 0; } int CVICALLBACK StopButtonCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: *(int *)callbackData=0; // Clear SerialFlag break; } return 0; }
I'd do something along these lines - saves tedious mucking about with threads.
02-15-2011 05:48 AM
Well, as already noted in your code the use of EVENT_IDLE is deprecated: you should use timer controls instead; the code can easily be changed to use a timer control.
02-15-2011 06:00 AM
... and has been for some while. However it seems to me that on the many occasions where you just want something to happen as fast as reasonably possible taking regard of all the other demands on the processors time EVENT_IDLE fits the bill perfectly. Is there a technical reason for its deprecation?
02-15-2011 07:56 AM
Well, I have no detailed informations about it.
I can think that timers are better implemented to accomplish regular tasks but do not pretend that this is an exhaustive reason.
Let's see if any CVI developer can elaborate on this subject.