473,594 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ 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.Threadin g
Imports System.Environm ent

'

Public Class Form1

'

Private Delegate Sub UpdateDelegate( )

Private theThreadOrNot, theUpdateOrNot As Boolean
Private theDelegate As UpdateDelegate
Private theReset As ManualResetEven t
Private theThread As Thread

'

Private Sub Form1_FormClosi ng(ByVal sender As Object, ByVal e As
System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing

'

If (theUpdateOrNot ) Then

'

theReset.Reset( )
theUpdateOrNot = False

Thread.Sleep(25 0)

End If

theThreadOrNot = True
theReset.Set()

If (theThread Is Nothing) Then

'

Exit Sub

End If

If (theThread.IsAl ive) Then

'

theThread.Join( )

End If

End Sub

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

'

theReset = New ManualResetEven t(False)

theDelegate = New UpdateDelegate( AddressOf _
OnUpdate)

theThread = New Thread(AddressO f _
OnThread)

With theThread

'

.IsBackground = True
.Priority = ThreadPriority. Normal

.Start()

End With

End Sub

Private Sub OnThread()

'

While (Not (theThreadOrNot ))

'

If (theReset.WaitO ne) Then

'

While (Not theThreadOrNot And theUpdateOrNot)

'

If (Me.InvokeRequi red) _
Then

'

Me.Invoke(theDe legate)

End If

End While

End If

End While

End Sub

Private Sub OnUpdate()

'

Me.Label.Text = TickCount.ToStr ing

End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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 1115
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*******@adel phia.neta écrit dans le message de
news: 11************* ********@l12g20 00...legro ups.com...
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.Threadin g
Imports System.Environm ent

'

Public Class Form1

'

Private Delegate Sub UpdateDelegate( )

Private theThreadOrNot, theUpdateOrNot As Boolean
Private theDelegate As UpdateDelegate
Private theReset As ManualResetEven t
Private theThread As Thread

'

Private Sub Form1_FormClosi ng(ByVal sender As Object, ByVal e As
System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing

'

If (theUpdateOrNot ) Then

'

theReset.Reset( )
theUpdateOrNot = False

Thread.Sleep(25 0)

End If

theThreadOrNot = True
theReset.Set()

If (theThread Is Nothing) Then

'

Exit Sub

End If

If (theThread.IsAl ive) Then

'

theThread.Join( )

End If

End Sub

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

'

theReset = New ManualResetEven t(False)

theDelegate = New UpdateDelegate( AddressOf _
OnUpdate)

theThread = New Thread(AddressO f _
OnThread)

With theThread

'

.IsBackground = True
.Priority = ThreadPriority. Normal

.Start()

End With

End Sub

Private Sub OnThread()

'

While (Not (theThreadOrNot ))

'

If (theReset.WaitO ne) Then

'

While (Not theThreadOrNot And theUpdateOrNot)

'

If (Me.InvokeRequi red) _
Then

'

Me.Invoke(theDe legate)

End If

End While

End If

End While

End Sub

Private Sub OnUpdate()

'

Me.Label.Text = TickCount.ToStr ing

End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Threadin g
Imports System.Environm ent

'

Public Class Form1

'

Private Delegate Sub UpdateDelegate( )

Private theThreadOrNot, theUpdateOrNot As Boolean
Private theDelegate As UpdateDelegate
Private theReset As ManualResetEven t
Private theThread As Thread

'

Private Sub Form1_FormClosi ng(ByVal sender As Object, ByVal e As
System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing

'

If (theUpdateOrNot ) Then

'

theReset.Reset( )
theUpdateOrNot = False

Thread.Sleep(25 0)

End If

theThreadOrNot = True
theReset.Set()

If (theThread Is Nothing) Then

'

Exit Sub

End If

If (theThread.IsAl ive) Then

'

theThread.Join( )

End If

End Sub

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

'

theReset = New ManualResetEven t(False)

theDelegate = New UpdateDelegate( AddressOf _
OnUpdate)

theThread = New Thread(AddressO f _
OnThread)

With theThread

'

.IsBackground = True
.Priority = ThreadPriority. Normal

.Start()

End With

End Sub

Private Sub OnThread()

'

While (Not (theThreadOrNot ))

'

If (theReset.WaitO ne) Then

'

While (Not theThreadOrNot And theUpdateOrNot)

'

If (Me.InvokeRequi red) _
Then

'

Me.Invoke(theDe legate)

End If

End While

End If

End While

End Sub

Private Sub OnUpdate()

'

Me.Label.Text = TickCount.ToStr ing

End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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
3226
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 behaviour. Inside the join() method of threading.Thread objects, a Condition named self.__block is acquired, and then the wait logic is executed. After the wait() finishes, self.__block is released and the method returns.
10
2164
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 query itself is built programmatically within my application, and example of which is below. I am hoping someone in the group may have some insight into why this query is so slow, suggesting perhaps a better structure for it, such that I can go...
11
2110
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 the Access 95 team). The person who did write it and put it in is no longer working at Microsoft (though that is actually Microsoft's loss!). But its a very creative poem, and a pretty funny one, too. There are a few lines in it that can always...
58
30204
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 neighborhood C# programmer? The standard answer I get from computer sales people is: "It means that the CPU can process 64 bits of data at a time instead of 32." Ok... I guess I *kind* of understand what that means at an intuitive level, but what...
2
8098
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 AcceptRejectRule is set to Cascade Table Structure: OrderID (Identity field) OrderDesc OrderParent (this is the self-join - it is a foreign key to OrderID) In my code I add several rows to the table, createing a hierarchy of orders
13
1663
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 Object and or Releases the unmanaged resources used by the Object and optionally releases
52
3191
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 works. Here's the setup. I have a function, called GetEmployeeCertifications, that is going to make a call to a SQL DB and return a result set. This module calls another function, called FillParameters, to determine if SQL parameters need to...
71
10706
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 is that automatic? Thanks
27
2831
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
7954
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8016
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8244
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6669
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5836
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3905
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1487
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1218
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.