Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / November 2006

Tip: Looking for answers? Try searching our database.

Why does Join method call sit there forever?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dachshund Digital - 28 Nov 2006 05:39 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.