Shut down thread - thread closes form, form doesn't close because thread calls it etc...

Shut down thread - thread closes form, form doesn't close because thread calls it etc...

Post by Robin Tuck » Sat, 18 Oct 2003 01:31:40



Some thread confusion to lighten up your day:

I have a worker thread and a main form. When the user clicks the Close icon
in the top right of the form, my program tells the thread to "stop". The
thread will then complete its current operation and "Invoke" a method on the
form telling it it has completed. The completed handler then calls Me.Close
(). The trouble is, I seem to have some deadlock here - I mean after I call
Me.Close(), does the form exit the method, return to the thread etc.?

How exactly can a worker thread shut down the form that created it!???

Any ideas?


Cheers


Robin
 
 
 

Shut down thread - thread closes form, form doesn't close because thread calls it etc...

Post by Robin Tuck » Sat, 18 Oct 2003 01:40:24

Its ok, nobody panic. I solved this one with an asynchronous call to the
main thread and a bit of fiddling about :)

"Robin Tucker" < XXXX@XXXXX.COM > wrote in

icon
the
Me.Close
call

 
 
 

Shut down thread - thread closes form, form doesn't close because thread calls it etc...

Post by Fergus Coo » Sat, 18 Oct 2003 03:07:27

Hi Robin,

Praise be that you've solved it!!. My heart's been pounding and my face
has been flushed and my mind's been all of a dither. I can calm down now!!
Aaaaahh. ;-))

Regards,
Fergus
 
 
 

Shut down thread - thread closes form, form doesn't close because thread calls it etc...

Post by scorpion53 » Sat, 18 Oct 2003 19:39:28

Hi Fergus,

Can you point me to a tutorial that will allow me to share controls among
threads? I am able to do datasets and such as long as I dont use them in the
primary thread but I would like to learn how to use controls otherwise if I
can.



face
 
 
 

Shut down thread - thread closes form, form doesn't close because thread calls it etc...

Post by Robin Tuck » Sat, 18 Oct 2003 20:03:52

You just need to use Delegate and the controls "Invoke" method. This will
ensure that the control method is executed on the controls thread, not the
thread making the call. Like this:

Here is a delegate, representing a method on my main form (doesn't matter
what it is, could be anything really) - this delegate is defined in my
thread.

Private Delegate Sub _onBeginNodeOperations_Delegate(ByVal nAdding As
Integer, ByVal OperationType _
As OperationsThreadState.NodeOperations, ByVal bIsUndoRedo As
Boolean)

Now, the thread wants to execute the delegate method on the main form
thread, not the current thread, so:

Try

m_Form.Invoke(New _onBeginNodeOperations_Delegate(AddressOf
m_Form.BeginNodeOperations), Parameters)

Catch ex As Exception

End Try




the
I


now!!