I am working on a project where I make use of the lp_solve5.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)
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
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)
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)
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)
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.