LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Cancelling an operation without polling.

Hello,

I am looking for an efficient way to quit a function. E.g. I have a Cancel Button on my panel. A callback function is executing, so if the user clicks the cancel button, nothing will happen until this function exits. I could call the GetUserEvent function in my callback function, but then i have to insert this code on several places in this function, and also in other functions that are called from this callback function. I have tried to poll the cancel button in a separate thread, but that did not seem to work...

Thanks for all responses.
0 Kudos
Message 1 of 3
(3,118 Views)
Wim,

You could do something like the following. Create a thread that wraps your function and launch that thread
during your callback. On your exit program callback you can simply terminate the thread manually. I suggest reading the function panel on CmtTerminateThreadPoolThread before using this option though as there are some caveats you should know about. Also make sure at some point to explicitly release your thread even if you don't end up terminating it during the process:

/*the CVI callback that you are using to launch a
int CVICALLBACK MyCallback(int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
/*Spin off a new thread that uses the function with a long delay in it*/
CmtScheduleThreadPoolFunction (tPool, Thread, NULL, &tID);

break;
}
return 0;
}

/*A thread containing your function that has a long delay*/
extern int CVICALLBACK Thread (void *Data)
{
/*Run your workhorse function here*/
}

/*The CVI callback for your exit button*/
int CVICALLBACK do_exit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
/*Terminate the long delay thread before exiting the program*/
CmtTerminateThreadPoolThread (tPool, tID, 0);
QuitUserInterface (0);
break;
}

}
Message 2 of 3
(3,110 Views)
Thanks a lot. This works great.

Wim
0 Kudos
Message 3 of 3
(3,101 Views)