Fred,
My thread adds nodes to a TreeView but to do this from another thread I had
to use the Invoke method. Am I correct in thinking that the Invoke method
then tries to run the delegate method in the main thread? If so what happens
if the main thread is waiting for this secondary thread to finish
(DocThread.Join). I presume this is the problem.
You are correct, Control.Invoke is trying to run something on the main
thread, the main thread is waiting on the secondary thread.
Welcome to multi-threaded programming. You have entered the state known as
Dead-lock!
What you could do is add the timeout parameter to the Join method, allowing
the main thread to "come up for air". If Join returned false, call DoEvents,
look back, so the Control.Invoke is allowed to finish... At least I believe
DoEvents will be required, as I think that is how Control.Invoke gets the
"delegate" to the main thread (via a Win32 message).
Something like:
Private Sub CancelUpdateAllNodeFonts()
If Not (DocThread Is Nothing) Then
If DocThread.IsAlive Then
CancelDocThread.Set() ' Set CancelThread to ask the thread to
stop.
Do Until DocThread.Join(500) ' try for half a second
Application.DoEvents()
Loop
I would probably limit the number of times I tried to Join the DocThread.
Hope this helps
Jay