LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do different threads know when to exit when I quit application?

Hi,
I have mutiple thread running in my application and they only exit when application(program) quits and because of variable scope I can not set flag when application is exiting. Is there anyway I can terminate all threads when my application exits? any callback or global flag to project etc.

Thanks for your help.
Sheetal
Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 1 of 4
(3,463 Views)
Hello Sheetal,

To terminate all the threads when you application ends, you need to follow the following process:

1) Change the value to a global variable/flag
2) Use CmtWaitForThreadPoolFunctionCompletion() to make sure that the program ends when all your threads end.
3) Keep checking in the thread the value for that global variable and terminate them once the value is changed.

This solution will make sure that your application ends when the threads end but not vice-a-versa. It is usually not recommended to forecfully terminate a thread from the program and thus closing the application when all threads close is a best option. In general when your application closes, all the child threads created by that application will as well be closed.

I hope it helps,

Rajiv
0 Kudos
Message 2 of 4
(3,463 Views)
Thanks for your help Rajiv.
But as I mentioned in my question I can't have global flag because of variable scope.
Any otherway you can think of? Like getting quitting application call back...?
Sheetal
Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 3 of 4
(3,463 Views)
Hello Sheetal,

I understand that you are not interested in using the global variables. But am little confused at what all you can and you cannot do.

Using globals would be the easiest and the best way to pass a flag to the threads. Other option is creating a global flag file and use a value inside that file. (kind of using global variable without using the memory location for the value).

And if you just want to make sure that threads close when the application is being closed. You can use CmtExitThreadPoolThread with the thread ID's to close each thread before actually ending the application.

I hope it helps this time...
Rajiv
0 Kudos
Message 4 of 4
(3,463 Views)