Terminating a thread in OnDestroy

Thread Solved

Join Date: Oct 2008
Posts: 35
Reputation: m610 is an unknown quantity at this point 
Solved Threads: 1
m610 m610 is offline Offline
Light Poster

Terminating a thread in OnDestroy

 
0
  #1
Mar 17th, 2009
I'm having a little trouble shutting down a thread that might or might not be executing when the program closes. The thread, actually there are two of them, is started in an OnTimer event, and in OnDestroy I stop the timers then I want to close the threads.

The thread code itself is pretty simple and this is what I have now.

Still, I get an access violation error, and when I use the WaitFor function I get lots of odd errors, such as invalid handle.

  1. // Constructor
  2. constructor TDaqThread.Create(OnTerminate:TNotifyEvent);
  3. begin
  4. inherited Create(true);
  5. Self.OnTerminate:=OnTerminate; // Defines the onTerminate event handler
  6. FreeOnTerminate:=true; // Thread frees itself when it terminates
  7. Resume; // Start the thread
  8. end;
  9.  
  10. // The thread executes a single procedure
  11. procedure TDaqThread.Execute;
  12. begin
  13. try
  14. MainForm.DoUpdateDaq;
  15. except
  16. on E:Exception do
  17. begin
  18. // Handle the error.
  19. end
  20. end;
  21. end;
  22.  
  23. // Here is how I create/start the thread. When it is done I run the
  24. // EndUpdateDaq procedure.
  25.  
  26. DaqThread:=TDaqThread.Create(EndUpdateDaq)
  27.  
  28. // At the beginning of the EndUpdateDaq procedure...
  29. procedure TMainForm.EndUpdateDaq(Sender:TObject);
  30. begin
  31. try
  32. // Has the Daq thread been terminated?
  33. if DaqThread<>nil then
  34. if DaqThread.Terminated then
  35. Exit;
  36.  
  37. // More stuff...
  38.  
  39. end;
  40.  
  41. Procedure TMainForm.OnDestroy(Sender:TObject);
  42. begin
  43. MonitorTimer.Enabled:=false;
  44.  
  45. if DaqThread <> nil then
  46. if not DaqThread.Terminated then
  47. begin
  48. DaqThread.OnTerminate:=nil;
  49. DaqThread.Terminate; // Stop the Daq thread
  50. // DaqThread.WaitFor; // Wait for it to terminate
  51. end;
  52.  
  53. // more stuff...
  54. end;
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: m610 is an unknown quantity at this point 
Solved Threads: 1
m610 m610 is offline Offline
Light Poster

Re: Terminating a thread in OnDestroy

 
0
  #2
Mar 23rd, 2009
I think I may have figured it out.

First, it turns out that if you set to thread to automatically free itself when it is done that you shouldn't ever mess with it again. Just fire and forget.

Instead of shutting it down I just enter a loop that waits for it to finish, then I move on to the next task. Of course the loop has a timeout.

To check if the thread is done I don't check directly with the thread because you never know when it will disappear, but rather I check a variable I set just before starting the thread and clear when it finishes it's onTerminate procedure.

I hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC