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

R

Robin Tucker

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
 
R

Robin Tucker

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

Fergus Cooney

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
 
S

scorpion53061

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.
 
R

Robin Tucker

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top