Delphi checks if TThread continues

I use TThread, I just need to find out if the thread is working, and stop the application if it takes a long time.

Example:

MyThread := TMyThread.Create; MyThread .Resume; MyThread.Terminate; total := 0; while MyThread.isRunning = true do // < i need something like this begin // show message to user to wait a little longer we are still doing stuff sleep(1000); total := total + 1000; if total > 60000 then exit; end; 

Is this possible with Delphi?

+7
multithreading delphi
source share
1 answer

The direct answer to your question is the Finished TThread property. However, I do not recommend doing this. This will lead you to this rather nasty Sleep based loop that you represent in the question.

There are several cleaning options, including at least the following:

  • Use WaitFor to block until the thread terminates. This will block the calling thread, which will prevent the user interface from displaying or respond to user requests.
  • Use MsgWaitForMultipleObjects in a loop, passing a stream handle. This allows you to wait for the thread to finish, but will also serve the user interface.
  • Implement an event handler for the OnTerminate event. This allows you to be notified of events triggered by events.

I recommend that you use one of these options instead of your home Sleep cycle. In my experience, option 3 usually results in the cleanest code. But it’s hard to give tough advice without knowing all the requirements. Only you have this knowledge.

+7
source share

All Articles