473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why does Join method call sit there forever?

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
4 1105
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
Did that... the MsgBox() method call is never done.

Nov 30 '06 #3

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Hansen | last post by:
I'm still trying to understand the behaviour that I'm seeing but I'm already pretty sure that it's either a bug, or something that would be considered a bug if it didn't perhaps avoid even worse...
10
by: Ike | last post by:
For some reason, I have a rather large (to me) query, with numerous inner joins, accessing a remote server, and it is taking about twenty times longer than most queries to the same database. The...
11
by: Michael \(michka\) Kaplan [MS] | last post by:
A little light humor... this "easter egg" was hidden deep in the Access 95 help system. I did not write it (I am not this creative and never was) and I did not put it in the product (I was not on...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
2
by: Ev | last post by:
I have a database table in SQL Server that has a self join. In C# I have a DataTable with a self-join. I have defined a foreign key constraint on the DataTable for the self join. The...
13
by: Dave | last post by:
Could someone explain to me when it is appropriate to call the Dispose() method that is contained in most .Net Framework Objects. The MSDN says that Dispose: Releases all resources used by the...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
27
by: Paulo da Silva | last post by:
Hi! I was told in this NG that string is obsolet. I should use str methods. So, how do I join a list of strings delimited by a given char, let's say ','? Old way:
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.