I'm probably going to be shot for giving away secrets, but hey...
 
 A can be solved easily if you get your form to create a Windows event (using the API call CreateEvent) and pass this as a parameter to your thread's constructor.  You thread can check this event periodically (by waiting on it for no time) and then commit suicide if the event is set.  This is a very safe way of ensuring that you are not referencing a thread that has already died.
 
Pass the owner as a parameter to the thread constructor.  This should allow you to set fOwner.fTherad := nil when the thread terminates.
 
Set FreeOnTerminate to false in your constructor.
 
Set FreeOnTerminate to true in the execute method when the thread has finished and after you have set fOwner.fThread to nil.
 
The form that started the thread can then (in its destructor) say:
  SetEvent(MyThreadDie);
  while assigned(fThread) do;
 
If the variable holding MyThreadDie is the event that you created when you instatiated the thread then by setting the event you can expect the thread to suicide.  You need to wait for it, not by MyThread.WaitFor, but by waiting until it has set its owner's reference to nil.  After having done that, it should free itself.
 
Trevor
----- Original Message -----
From: Ross Levis
Sent: Thursday, October 16, 2003 2:18 PM
Subject: Re: [DUG]: Threads again

Thanks for the details Trevor.  I think I'll give it a go.
 
Cheers,
Ross.
----- Original Message -----
Sent: Thursday, October 16, 2003 2:01 PM
Subject: Re: [DUG]: Threads again

Ross,
  My experience with components and threads is that you should be able to use the Indy Component within your thread without calling synchronize provided:
 
  a) you manage form destruction well, so that the component can't be destroyed while the thread is working with it.
  b) you do not have any event handlers in the component that call methods on the form (This is a great way to make things go BANG).
  c) there is no other code in the form which works with the Indy Component; it must all live within the thread.
  d) there is no code in the component which references its owner while the thread is fiddling with it.
  e) the component itself does not rely on non-threadsafe behaviour.
 
I haven't done a lot with Indy in threads so I don't know how safe it is going to be to use, but I have done a whole bunch with IBX in threads and the results have been great.
 
HTH
 
Trevor
----- Original Message -----
From: Ross Levis
Sent: Thursday, October 16, 2003 1:13 PM
Subject: [DUG]: Threads again

(D5) I currently have a thread which is updating a text file.  I'm wanting to alter it to optionally send the file to an ftp server once it's created.  I've written to ftp servers before using Indy.
 
I need to put the Indy component on a form, so if I put it on the mainform, can I use it in the thread without using Synchronize?.  Or is Indy quite responsive and would not slow down the main thread by using Synchronize?
 
Any ideas?
 
Regards,
Ross Levis.

Reply via email to