LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Terminating the program from a spawned thread?

I have a test executive that gets launched in its own thread when the user hits a "Start" button on the UIR. The executive runs all the tests and determines if the tests passed/failed. Finally, it gives the user the option to retest, test a new object, or exit. If the user selects retest, the thread stops and is terminated normally. If the "test new" option is selected, the main UIR is hidden and a serial number UIR pops up. If the "exit" option is selected, the executive should terminate.

I'm having problems with the "new" and "exit" options. With the "new" option, the serial number UIR doesn't always appear. With the "exit" option, the executive lives on. (Apparently, running QuitUserInterface(0) from the thread doesn't work.)

To solve my problem, I think I need to have my spawned thread launch the "show serial number UIR" and "Quit User Interface" functions in the "main" thread. Is there an elegant way to do this?

- Nobody

(Hook'em Horns!)
0 Kudos
Message 1 of 4
(3,117 Views)

Hi IB,

  If I understand what you are describing properly, then I would recommend that you use a thread callback when your test executive thread completes its test.  The thread callback can be run in the thread that started your GUI, which is most likely the same thread that scheduled the test executive thread.  Thus you have better control over exiting the application or restarting the test executive thread if more testing is desired.  I would probably put the serial number entry stuff in a popup before the test thread is even scheduled (so that the tests could still be canceled one last time before they begin).  There are a couple of different approaches to pass the serial number into the test thread, if that is even truly a requirement for the test to run properly.

  Look to CmtScheduleThreadPoolFunctionAdv().  You can typically use DEFAULT_THREAD_POOL_HANDLE for small applications unless the thread priorities for the test thread need to be adjusted.  Specify a thread callback function, and get that setup, and then leave your event callback mask set to EVENT_TP_THREAD_FUNCTION_END and CmtGetCurrentThreadID() for the callback thread.

Good Luck,

Orlan

(Gig'em Aggies!)

0 Kudos
Message 2 of 4
(3,107 Views)
 

Sincronizing execution betwenn more threads needs some special attentions to permit save execution to all of them (preparation, executin, stop and resource clearing and so on. I suggest you take a look to this document that explains with high detail all what's involved in developing multithreaded applications. Monitoring and Controlling Secondary Threads paragraph can be very helpful in your application.

As per serial number prompting, you could use PostDeferredCall function which posts a calls to a function into the main thread (the one into which the main function and ordinarily all the user interface is executed). Just for completeness, PostDeferredCallToThread posts a call to a function in a thread other from the main one.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 4
(3,106 Views)
PostDeferredCall worked. It was exactly what I needed. I just created two functions (DeferredLogin and DeferredQuit). My spawned thread called these deferred functions with PostDeferredCall. The runtime engine picked these function calls up in the main thread and terminated or opened my SN window.

Many thanks,

Nobody
0 Kudos
Message 4 of 4
(3,083 Views)