PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Mark Strickland on June 06, 2005, 02:47:47 AM

Title: Ending the main form from a THREAD
Post by: Mark Strickland on June 06, 2005, 02:47:47 AM
I have a program that has a THREAD running to do some Internet SSL transactions.  I have a button on the main form to end that sets a flag that the THREAD recognizes and ends.  The button code waits for the THREAD to end and then does a FF_CLOSEFORM to exit the program.  This all works fine.

The main purpose of the form is to simply display messages in a ListBox added by the THREAD.  All of that works fine.

I also can have the thread detect on it's own that a shutdown is needed and it then needs to close the form right before it terminates.  I tried putting a FF_CLOSEFORM in the THREAD but the program does not end.

Any thoughts? --- I am sure it is something "silly".

Thanks,

Mark
Title: Ending the main form from a THREAD
Post by: Roger Garstang on June 06, 2005, 06:58:18 PM
I believe this is probably the same problem I always run into when using the CUSTOM function for everything.  I have a loop or thread running on button click and when I click Close it sometimes exits, but still is running in task manager.  It is something about how it is exiting that if you close from within a thread or loop..especially within a message function it doesn't completely exit.

I usually have a Global I call Running now that is 0 when not running, 1 when running, 2 when Cancel is clicked, and 3 which it is set to if Close is clicked...and Close is aborted.  My running loop then checks for running > 1 and if it is 2 the loop just exits, if 3 then it exits then sets Running= 0 before a PostMessage sending WM_CLOSE again...which then exits cause it checks for Running to = 0.
Title: Figured out a solution
Post by: Mark Strickland on June 06, 2005, 07:16:42 PM
I figured out a solution.  I set a timer on the main form and watch for a global flag set by the THREAD when it ends.  There is logic in the THREAD to detect a special case to end.  Once the flag has the proper value I do all of the cleanup and exit in the timer just like I do when I click the EXIT button.

Now that I think about it I could have added the special detection logic in the timer but that might be out of synch with what the THREAD was doing.

Yep THREADS just keep running when a program ends.  Great way to create a memory/resource leak.  I believe they are in the same address space as the main program but are dispatched by the OS in a separate task.  It would seem there should be a WinAPI function to terminate a THREAD but I don't believe there is such a function.  Because of that you must make THREADS self ending with some logic.  Then you can check if the THREAD is still running from the main program then exit.

Roger, thanks for the reply.

Mark