LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

how to exit from button callback function before it complete execution in LabwindowsCVI

Solved!
Go to solution

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

0 Kudos
Message 1 of 14
(4,479 Views)
Solution
Accepted by topic author Vkumar

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....

-----------------------------------------
Kobi Kalif
Software Engineer

0 Kudos
Message 2 of 14
(4,477 Views)

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

0 Kudos
Message 3 of 14
(4,473 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 14
(4,467 Views)

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..

-----------------------------------------
Kobi Kalif
Software Engineer

0 Kudos
Message 5 of 14
(4,466 Views)

Thanks both of you. Now i am clear.

 

--Vishnu

0 Kudos
Message 6 of 14
(4,461 Views)

 

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.

 

0 Kudos
Message 7 of 14
(4,455 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 14
(4,447 Views)

... 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?

0 Kudos
Message 9 of 14
(4,443 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 10 of 14
(4,416 Views)