|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why does Join method call sit there forever?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 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" <Schors***@adelphia.net> a écrit dans le message de news: 1164692386.776672.62***@l12g2000cwl.googlegroups.com...Show quoteHide quote > 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 > 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. 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: Show quoteHide quote > 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
Datagridview...cell contents
Newbie Question here Newbie help: How to detect when a worker thread ihas completed normaly word automation vb.net How broad should an interface be? Advice needed. set refresh rate for an active browser window newbie syntax question: adding event handler for button not working File and Database Search Re: Is VB.NET Stable?? Error on deleting registry value |
|||||||||||||||||||||||