App freezing on Quit, when a Thread was running

I have a Thread, which resides in a Folder within another Folder in the Xojo IDE.
In this Thread, opens connections to 3 MySQLCommunityServer’s (over LAN and sometimes oven VPN, this works fine) and pull data which is stored in Strings within a Module in the same IDE Folder. The resulting RecordSets are processed in While…Wend loops.
Each Database connection is closed, right after the RecordSet has it’s data.
Each RecordSet is closed, right after i’ve processed the containing data in my While…Wend Loops.

These Strings then fill a ListBox in my Main Window (the only opened window at that time). This is done via a Timer in my Main Window. This Timer pulls the 1st entry of each String-Array until the Thread has the Status 4 and until the Arrays are empty.

When i click on the Close Icon in the Main Window, the cancel close event fires and a Method within the Main Windows is called, which checks if an instance of this Thread in my Main Window is <> NIL. If it’s <> NIL it kills the instance with the Thread.Kill function.

I am pretty sure, i do not interact with any GUI related data in my Threads.

My issue is; if i close the App while such a Thread was running, my App freezes. I have the feeling, the this happens only if a Thread was running, which opened more then 1 Database connection. But i am not sure about this…

Try sending a message to your thread to end it’s work gracefully and then you quit() only after that. End timers as necessary too.

Thank you Rick. This helps a lot.

Now i catch via CancelClose the closing of my Main Window and set a Boolean in a Module, which indicates that the App wants to quit.
Then my Threads check on every loop, if this Boolean is set to True. If it is TRUE, the Thread will end its work gracefully.

At the same time, a Timer is checking if any Thread is still running and sends a Kill to these Threads. When all Threads have stopped, the timer ends and Quits the App.

A thread could kill itself after receiving your message and ending it’s job properly. If you have multiple threads, you could maintain a shared property (module global) like CurrentRunningThreads incrementing on thread.runs and decremented by the threads just before self.kill()
When CurrentRunningThreads < 1 you are good to go. :wink:

Thank you so much. That’l make thing MUCH easier! :slight_smile: