CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Detecting hanging thread and abort it

    I am working on a project where I make use of the lp_solve 5.5.0.13 DLL to solve many integer programming (IP) problems in succession. I set the timeout to 1s in order to speed up the calculations. Sometimes the solving procedure hangs.

    In order to account for this I would like to start the call to the DLL to solve the IP in a new thread. That is easy. I would like to kill the thread if it hangs in order to make my program more robust. I would like to do so by timing the thread. If the thread is still active after, say 2s (1s more than my timeout limit), I would like to abort it and continue with the remaining calculations.

    How should I go about doing this without waiting 2s for the threads that do not hang? The current threat should also not continue until the new thread (calculating the IP) is closed. How do I do this?
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Detecting hanging thread and abort it

    This is one way that i can think of doing it..

    For each new thread that you spawn, simply start a 2000ms timer for it, if the timer tick's and the thread is still running,kill it..

    On the Fly code:
    Code:
        Public WithEvents ThreadTimer As Windows.Forms.Timer
    
        Public Sub CallCalkThread()
            ThreadTimer = New Windows.Forms.Timer
            'Start thread
            ThreadTimer.Interval = 2000
            ThreadTimer.Enabled = True
            'do other stuff..
        End Sub
    
        Private Sub ThreadTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ThreadTimer.Tick
            'if thread still running .. then kill thread.
        End Sub
    Hope this helps...

    Gremmy.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: Detecting hanging thread and abort it

    Thanks Gremlin, I appreciate the help. Will try it.
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  4. #4
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: Detecting hanging thread and abort it

    I tried the following code, but it does not abort the thread when it should. Any ideas why?

    I created a simple form in SharpDevelop with a button and added this code:

    Code:
    Option Explicit On
    Option Strict On
    
    Imports System.Threading
    
    Public Partial Class MainForm
    	
    	
    	Public Sub New()
    		' The Me.InitializeComponent call is required for Windows Forms designer support.
    		Me.InitializeComponent()
    	End Sub
    
    
    	Private WithEvents ThreadTimer As New System.Windows.Forms.Timer()
    	Private Shared ExitFlag As Boolean = False
    	Private Shared t As Threading.Thread
    	Private Shared Aborted as Boolean
    
    
    	Sub ButtonClick(sender As Object, e As EventArgs)
    		ExitFlag = False
    		Aborted = False
    
    		t = new Thread(AddressOf PauseThread)
    		ThreadTimer.Interval = 2000
    		ThreadTimer.Start()
    		t.Start()
    
    		Do While ExitFlag = False
    		Loop
    
    		If t.IsAlive Then
    			t.Abort()
    		End If
    		
    		If Aborted Then
    			MsgBox("Program aborted correctly")
    		Else
    			MsgBox("Completed the task without aborting")
    		End If
    		
    	End Sub
    	
    
    	Private Sub PauseThread()
    		t.Sleep(5000)
    		ExitFlag = True
    	End Sub
    
    
    	Private Sub ThreadTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) handles ThreadTimer.Tick
    		If t.IsAlive Then
    			t.Abort()
    			Aborted = True
    		End If
    		ExitFlag = True
    	End Sub
    	
    	
    End Class
    Is the while ExitFlag = False loop the best way to force the subroutine to wait for the other thread to complete?
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  5. #5
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: Detecting hanging thread and abort it

    Here is the VB.NET project that performs this test.
    Attached Files Attached Files
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Detecting hanging thread and abort it

    Where are you setting the Exit condition? That should be in the Timer event, when the count is up.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: Detecting hanging thread and abort it

    Quote Originally Posted by dglienna View Post
    Where are you setting the Exit condition? That should be in the Timer event, when the count is up.
    Sorry, I'm not sure what you mean. I assume that when the time (2000ms) is up it triggers the ThreadTimer.Tick event. When this happens an if-statement checks whether or not the thread is still running. If the thread is still running it is aborted (I assume this is the correct method of killing a thread).

    The ExitFlag boolean then makes a note that the thread has completed its task and the other subroutine may continue with the calculations.

    Is the t.IsAlive call the correct method of checking whether or not a thread is running? Even so, if I add a breakpoint in the subroutine that is supposed to trigger after 2s, the program never seems to get there.

    What's wrong here?
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  8. #8
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: Detecting hanging thread and abort it

    Ah ha! I have managed to find a way to do what I need using the Thread.Join method. Below is the code:

    Code:
    Option Explicit On
    Option Strict On
    
    Imports System.Threading
    
    Public Partial Class MainForm
    	
    	
    	Public Sub New()
    		' The Me.InitializeComponent call is required for Windows Forms designer support.
    		Me.InitializeComponent()
    	End Sub
    
    
    	Private Shared ExitFlag As Boolean = False
    	Private Shared t As Threading.Thread
    	Private Shared Aborted as Boolean
    
    
    	Sub ButtonClick(sender As Object, e As EventArgs)
    		ExitFlag = False
    		Aborted = False
    
    		t = new Thread(AddressOf PauseThread)
    		t.Start()
    		
    		Do While ExitFlag = False
    			If Not t.Join(2000) Then
    				ExitFlag = True
    				t.Abort()
    				Aborted = True
    			End If
    		Loop
    
    		If Aborted Then
    			MsgBox("Program aborted correctly")
    		Else
    			MsgBox("Completed the task without aborting")
    		End If
    		
    	End Sub
    	
    
    	Private Sub PauseThread()
    		t.Sleep(5000)
    		ExitFlag = True
    	End Sub
    
    
    End Class
    This still does not explain why the timer idea did not work though. Will hold off on calling this thread resolved until we can figure out why that is.
    Last edited by FrankZA; November 14th, 2008 at 03:31 AM. Reason: Added link to Thread.Join method explanation on MSDN
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured