hi,
I am new to multi-threading and need some help. Currently, i am seeing some weird behaviour in my program and do not understand why. I also have some questions at the bottom.
i have an active x richtextbox.fp, easytab.fp, async timer.fp and msie.fp
// still in main thread
// a picture button also calls this function
void CVICALLBACK Execution (int menubar, int menuItem, void *callbackData, int panel)
{
// Do some stuff
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ExecutionThread, NULL, &ExeThreadID);
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, ExeThreadID, OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, ExeThreadID);
// Updates Stuff
}
int CVICALLBACK ExecutionThread (void *functionData)
{
// Creates Async Timer
TimerHandle = NewAsyncTimer (TimerInterval, 1, 1, ExecutionTimer, NULL);
// go to function
execution ()
return 0;
}
int CVICALLBACK ExecutionTimer (int reserved, int theTimerId, int event, void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_TIMER_TICK)
{
if (TimerHandle == theTimerId)
{
TimerFlag = 1;
}
}
return 0;
}
void execution (void)
{
while StopFlag == 0
{
if TimerFlag == 0
//does some stuff
else
PostDeferredCallToThreadAndWait (ExeTimeUp, 0, CmtGetMainThreadID(), POST_CALL_WAIT_TIMEOUT_INFINITE);
}
}
void CVICALLBACK ExeTimeUp (void *callbackData)
{
Status = ConfirmPopup ("Timer", "Time is up!\n Would you like to continue?");
if (Status == 1)
{
TimerFlag = 0;
TimerHandle = NewAsyncTimer (TimerInterval, 1, 1, ExecutionTimer, NULL);
}
else
{
MessagePopup ("Program", "Program Terminated!");
TimerFlag = 0;
StopFlag = 1;
}
}
// a picture button also calls this function
void CVICALLBACK Stop (int menubar, int menuItem, void *callbackData, int panel)
{
StopFlag == 1
}
During execution, user is only allowed to press on the Stop button (calls stop callback).
Problem 1:the timer confirm popup panel appears much later after timer flag is set to high and it is not because of the while loop.
Problem 2: sometimes if user clicks on the stop button, the thread is terminated (Look from the variables window>>run>>thread) but the program is still waiting at CmtWaitForThreadPoolFunctionCompletion.
Question 1: can CmtWaitForThreadPoolFunctionCompletion actually receives the PostDeferredCallToThreadAndWait immediately?
Not sure if this is the correct way to write the program.
Thanks in advance, any help or advice would be much appreciated.
Cheers
AL