LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending message to secondary thread to quit

I have 2 buttons on my main panel. Pressing one, I start a secondary thread,
using CmtScheduleThreadPoolFunction function. The secondary thread is just
calling Wait, where Wait is:

void Wait(double NumOfSeconds)
{
double t0;
double t1;

t0 = Timer();
t1 = Timer();

while ((t1-t0) < NumOfSeconds) {
ProcessSystemEvents();
t1 = Timer();
}
}

Now when I press the second button on Main GUI, I want to stop this
secondary thread. I know I can use global valiables which I can set in main
thread and check oftne in secondary thread, but I am looking for doing by
sending a message to the secondary thread. So I tried
PostDeferredCallToThread funtion, like:

PostDeferredCallToThread(QuitSecondaryThreadFunction, 0,
Secondary_Thread_I
d)

and

void CVICALLBACK QuitSecondaryThreadFunction(void* pFunctionData)
{
CmtExitThreadPoolThread(1); <------- is this correct,How do I call it from
my secondary thread
}

When I do that, I see that my secondary thread doesn't process this message.
The secondary thread is calling ProcessSystemEvents while in wait, so it
should process the message, isn't it?

I am just starting in this so any help is appreciated.

vishi
0 Kudos
Message 1 of 5
(3,355 Views)
Hi Vishi,

As I look at your code, the problem might come from the secondary_thread_id. You are using the thread function ID which is different from the thread ID. You need to obtain the thread ID as follows:

CmtGetThreadPoolFunctionAttribute (poolHandle, secondary_thread_id, ATTR_TP_FUNCTION_THREAD_ID, &threadID);

Then use the threadID to call
PostDeferredCallToThread(QuitSecondayThreadFunction, 0, threadID);

This will work (it did for me).

Good luck,
Azucena
0 Kudos
Message 2 of 5
(3,355 Views)
Thanks for pointing the mistake. I tried it but still running into problems.
I will look more into it.


vishi

"aperez" wrote in message
news:506500000005000000EDC00000-1031838699000@exchange.ni.com...
> Hi Vishi,
>
> As I look at your code, the problem might come from the
> secondary_thread_id. You are using the thread function ID which is
> different from the thread ID. You need to obtain the thread ID as
> follows:
>
> CmtGetThreadPoolFunctionAttribute (poolHandle, secondary_thread_id,
> ATTR_TP_FUNCTION_THREAD_ID, &threadID);
>
> Then use the threadID to call
> PostDeferredCallToThread(QuitSecondayThreadFunction, 0, threadID);
>
> This will work (it did for me).
>
> Good luck,
> Azucena
0 Kudos
Message 3 of 5
(3,355 Views)
Hello Vishi

I would prefer to use a global variable, check it in the while loop and
return, when the variable is set.

e.g.
volatile BOOL bStopSecond = FALSE;

void Wait(double NumOfSeconds)
{
bStopSecond = FALSE;
...
while ()
{
...
if (bStopSecond) return:
}
....

To stop the second thread, you only must set the variable to TRUE.

Stephan


"Vishi Anand" schrieb im Newsbeitrag
news:3e16276d@newsgroups....
> I have 2 buttons on my main panel. Pressing one, I start a secondary
thread,
> using CmtScheduleThreadPoolFunction function. The secondary thread is just
> calling Wait, where Wait is:
>
> void Wait(double NumOfSeconds)
> {
> double t0;
> double t1;
>
> t0 = Timer();
> t1 = Timer();
>
> whi
le ((t1-t0) < NumOfSeconds) {
> ProcessSystemEvents();
> t1 = Timer();
> }
> }
>
> Now when I press the second button on Main GUI, I want to stop this
> secondary thread. I know I can use global valiables which I can set in
main
> thread and check oftne in secondary thread, but I am looking for doing by
> sending a message to the secondary thread. So I tried
> PostDeferredCallToThread funtion, like:
>
> PostDeferredCallToThread(QuitSecondaryThreadFunction, 0,
> Secondary_Thread_Id)
>
> and
>
> void CVICALLBACK QuitSecondaryThreadFunction(void* pFunctionData)
> {
> CmtExitThreadPoolThread(1); <------- is this correct,How do I call it
from
> my secondary thread
> }
>
> When I do that, I see that my secondary thread doesn't process this
message.
> The secondary thread is calling ProcessSystemEvents while in wait, so it
> should process the message, isn't it?
>
> I am just starting in this so any help is appreciated.
>
> vishi
>
>
>
>
0 Kudos
Message 4 of 5
(3,355 Views)
Thanks, that's what I did and it is working fine.

vishi

"Stephan Gerhards" wrote in message
news:3e1a8d2d@newsgroups....
> Hello Vishi
>
> I would prefer to use a global variable, check it in the while loop and
> return, when the variable is set.
>
> e.g.
> volatile BOOL bStopSecond = FALSE;
>
> void Wait(double NumOfSeconds)
> {
> bStopSecond = FALSE;
> ...
> while ()
> {
> ...
> if (bStopSecond) return:
> }
> ....
>
> To stop the second thread, you only must set the variable to TRUE.
>
> Stephan
>
>
> "Vishi Anand" schrieb im Newsbeitrag
> news:3e16276d@newsgroups....
> > I have 2 buttons on my main panel. Pressing one, I start a secondary
> thread,
> > using CmtScheduleThreadPoolFunction function. The secondary thread is
just
> > calling Wait, where Wait is:
> >
> > void Wait(double NumOfSeconds)
> > {
> > double t0;
> > double t1;
> >
> > t0 = Timer();
> > t1 = Timer();
> >
> > while ((t1-t0) < NumOfSeconds) {
> > ProcessSystemEvents();
> > t1 = Timer();
> > }
> > }
> >
> > Now when I press the second button on Main GUI, I want to stop this
> > secondary thread. I know I can use global valiables which I can set in
> main
> > thread and check oftne in secondary thread, but I am looking for doing
by
> > sending a message to the secondary thread. So I tried
> > PostDeferredCallToThread funtion, like:
> >
> > PostDeferredCallToThread(QuitSecondaryThreadFunction, 0,
> > Secondary_Thread_Id)
> >
> > and
> >
> > void CVICALLBACK QuitSecondaryThreadFunction(void* pFunctionData)
> > {
> > CmtExitThreadPoolThread(1); <------- is this correct,How do I call it
> from
> > my secondary thread
> > }
> >
> > When I do that, I see that my secondary thread doesn't process this
> message.
> > The secondary thread is calling ProcessSystemEvents while in wait, so it
> > should process the message, isn't it?
> >
> > I am just starting in this so any help is appreciated.
> >
> > vishi
> >
> >
> >
> >
>
>
0 Kudos
Message 5 of 5
(3,355 Views)