LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to pause/stop an execution of a thread?

I have a thread which is executing a whole set of instructions and an async timer running at the same time. After a certain amount of time have passed 'event timer tick', i would like to pause the execution thread and a popup will appear asking user to stop execution or to continue. Is this possible?

cheers
AL
0 Kudos
Message 1 of 9
(5,163 Views)
Have a look at SuspendThread() and ResumeThread() (Windows SDK)
-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 2 of 9
(5,139 Views)
I have found through experience that running multiple threads in an application is good, easy, and if you are halfway intelligent in the design, you don't need to worry about thread-safe variables. Where I doing what you are doing (and I may have once or twice), I would do something like this (note, the following is not code!):

Async Timer Callback Function()
{
timerEvent = 1;
}

Thread function
{
while (RunThreadFlag)
{
do a bunch of stuff;
if (timerEvent)
ContinueFlag = DisplayDecisionPopup;
if (!ContinueFlag)
RunThreadFlag = 0;
}
}

Message Edited by hendra@ngms on 03-03-2005 03:38 PM

0 Kudos
Message 3 of 9
(5,125 Views)

Message Edited by hendra@ngms on 03-03-2005 03:37 PM

0 Kudos
Message 4 of 9
(5,125 Views)
Hi hendra,

thanks for replying, from what i gather of the sample that you have posted, it seems that i have to do something first then detect if timer event occured followed by the popup. This is not exactly what i wanted. Look below to see a sample of what i think the code should be like. Not sure if it's the correct way to write a code though. 😃

async timer
{
timer event occured
{
stop timer
pause thread function 1
prompt popup

if continue, resume thread function 1 from where it was paused earlier and resume timer again
else, terminate thread function 1
}
}

thread fucntion 1
{
go to execution function
}


Not sure if i can write it this way. Any advice given would be much appreciated. i'm a new user to thread functions.

Thanks CVI_User, i'll try looking into suspend thread and resume thread... not too sure about windows sdk though.

cheers
AL
0 Kudos
Message 5 of 9
(5,117 Views)
hmmm, just a thought....

how is it that tab is not allowed in message body and lots of space bars to represent tab spacing is automatically truncated away?

it makes a reader harder to understand sample codes that user post up.

cheers
AL
0 Kudos
Message 6 of 9
(5,117 Views)
This compiles, and should work, perhaps with a little fine tuning. If you want to see this with indents, send me your e-mail address -- gary.hendra@ngc.com

#include "asynctmr.h"
#include
#include
#include
#include "testthread.h"

static int panelHandle, runThread, timerExpired, timerID;

//-----------------------------------------------------------------
// Function Declarations
int CVICALLBACK RespondToTimer(int reserved, int timerId, int event,
void *callbackData, int eventData1,
int eventData2);
int CVICALLBACK doSomeStuffThread (void *functionData);
//-----------------------------------------------------------------


int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "testthread.uir", PANEL)) < 0)
return -1;

timerID = NewAsyncTimer (10.0, -1, 1, RespondToTimer, 0);

runThread = 1;
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE,
doSomeStuffThread, NULL, NULL);

DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}


int CVICALLBACK RespondToTimer(int reserved, int timerId, int event,
void *callbackData, int eventData1,
int eventData2)
{
SuspendAsyncTimerCallbacks ();
return timerExpired = 1;
}



int CVICALLBACK doSomeStuffThread (void *functionData)
{
int popupReturnVal;

while(runThread)
{
// Do Some Stuff;
if (timerExpired)
{
popupReturnVal = GenericMessagePopup ("Do you want to continue?",
"Continue the Thread?", "No",
"Yes", "", 0, 0, 0,
VAL_GENERIC_POPUP_BTN1,
VAL_GENERIC_POPUP_BTN1,
VAL_GENERIC_POPUP_BTN3);
if (popupReturnVal == 0)
runThread = 0;
else
ResumeAsyncTimerCallbacks ();

}
}
return 0;
}
0 Kudos
Message 7 of 9
(5,100 Views)
Hi hendra,

i did some modifications to your earlier program and it also works!! Thanks alot for your patience and help!!

cheers
AL
0 Kudos
Message 8 of 9
(5,076 Views)
You are very welcome, Al. I like sharing my meager knowledge on this subject.....
0 Kudos
Message 9 of 9
(5,067 Views)