Why does Join method call sit there forever? This code works,
including the delegate call, but if the join method is ever called, it
seems the main thread blocks, and it is hung. HELP! This is driving
me nuts!
-----------------------------------------------------------------------------------
Imports System
Imports System.Threading
Imports System.Environment
'
Public Class Form1
'
Private Delegate Sub UpdateDelegate()
Private theThreadOrNot, theUpdateOrNot As Boolean
Private theDelegate As UpdateDelegate
Private theReset As ManualResetEvent
Private theThread As Thread
'
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'
If (theUpdateOrNot) Then
'
theReset.Reset()
theUpdateOrNot = False
Thread.Sleep(250)
End If
theThreadOrNot = True
theReset.Set()
If (theThread Is Nothing) Then
'
Exit Sub
End If
If (theThread.IsAlive) Then
'
theThread.Join()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'
theReset = New ManualResetEvent(False)
theDelegate = New UpdateDelegate(AddressOf _
OnUpdate)
theThread = New Thread(AddressOf _
OnThread)
With theThread
'
.IsBackground = True
.Priority = ThreadPriority.Normal
.Start()
End With
End Sub
Private Sub OnThread()
'
While (Not (theThreadOrNot))
'
If (theReset.WaitOne) Then
'
While (Not theThreadOrNot And theUpdateOrNot)
'
If (Me.InvokeRequired) _
Then
'
Me.Invoke(theDelegate)
End If
End While
End If
End While
End Sub
Private Sub OnUpdate()
'
Me.Label.Text = TickCount.ToString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If (theUpdateOrNot) Then
'
theReset.Reset()
theUpdateOrNot = False
Else
'
theReset.Set()
theUpdateOrNot = True
End If
End Sub
End Class
Patrice - 28 Nov 2006 12:03 GMT
My first move would be to check if I actually reach the end of the thread
(you could add a trace or whatever to see if you are still looping).
> Why does Join method call sit there forever? This code works,
> including the delegate call, but if the join method is ever called, it
[quoted text clipped - 148 lines]
>
> End Class
Dachshund Digital - 30 Nov 2006 04:08 GMT
Did that... the MsgBox() method call is never done.
Dachshund Digital - 30 Nov 2006 05:25 GMT
I did figure out part of the problem. If I call join in the
FormClosing event, it appears the thread blocks its-self and the join
never seeing the thread complete, keeps waiting, effectively forever.
Given this new information... I found a couple of references to similar
problems .NET.
If I Disable the FormClosing event until the user explicitly clicks the
button to stop the thread processing, then enable the FormClosing event
processing, I never get Join to hang.
This has to be something with the why the FormClosing event is
implemented.
Brian Gideon - 30 Nov 2006 13:54 GMT
Dachshund,
This is a common deadlock scenario. The UI thread calls Join on the
worker thread, but the worker thread needs to Invoke a method on the UI
thread before it can complete. Deadlock! The UI thread is blocking on
the theThread.Join call and the worker thread is blocking on the
Me.Invoke call. It really has very little to do with the FormClosing
event specifically.
Brian
> Why does Join method call sit there forever? This code works,
> including the delegate call, but if the join method is ever called, it
[quoted text clipped - 148 lines]
>
> End Class