By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Why does Join method call sit there forever?

P: n/a
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

Nov 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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).

--

"Dachshund Digital" <Sc*******@adelphia.neta écrit dans le message de
news: 11*********************@l12g2000cwl.googlegroups.c om...
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

Nov 28 '06 #2

P: n/a
Did that... the MsgBox() method call is never done.

Nov 30 '06 #3

P: n/a

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.

Nov 30 '06 #4

P: n/a
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

Dachshund Digital wrote:
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
Nov 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.