G
Guest
I need to have a thread run as long as it needs to, and not stop until it is cancelled by either closing the application or manually be stopped. It is simply pinging a machine to see what the connection speed is. The ping code is not the problem, as I have used it to get the speeds before, and it is solid
What happens is that after the thread has been running awhile, it just stops. I cannot stop it, but when I restart the thread, it is fine. I don't mind restarting the thread, but I want it to do it automatically. Is there some code that I can put in to detect if the thread has gone awry?
I have a button on the form that simply starts and stops the thread. If I leave the thread running too long, the code stops to work. When that happens, I stop the thread, and I get the 'Thread cannot be stopped' message box (see code below). Then I can restart the thread
I used the code samples that the MSDN has to make it work. Here is the code I have now
Public CancelThread As New System.Threading.ManualResetEvent(False
Public ThreadisCanceled As New System.Threading.ManualResetEvent(False
Private Sub SomeLongTask(
' Run code until CancelThread is set
While Not CancelThread.WaitOne(0, False)
' Do some kind of task here
System.Threading.Thread.Sleep(CSng(txtPingDelay.Text) * 1000) 'txtpingdelay = .
ThreadPing(
End Whil
If CancelThread.WaitOne(0, False) The
'Acknowledge that the ManualResetEvent CancelThread is set
ThreadisCanceled.Set(
End I
End Su
Public Sub StartTask(
' Starts a new thread
Dim th As New System.Threading.Thread(AddressOf SomeLongTask
CancelThread.Reset(
ThreadisCanceled.Reset(
th.Start(
End Su
Public Sub CancelTask(
' Stops any threads started by the StartTask procedure
' Notice that this thread both receives and sends
' synchronization events to coordiante the threads.
CancelThread.Set() ' Set CancelThread to ask the thread to stop
If ThreadisCanceled.WaitOne(4000, False) The
' Wait up to 4 seconds for the thread to
' acknowledge that it has stopped
Els
MsgBox("The thread could not be stopped."
End I
End Su
Private Sub ThreadPing(
Dim Sum As Int32 =
Dim myPinger As IDOTPing.Ping = New IDOTPing.Pin
Dim ret As Int3
If txtHost.Text.Length < 1 The
ret = myPinger.PingHost("localhost"
Els
ret = myPinger.PingHost(txtHost.Text
End I
PingAverage.Enqueue(ret
If PingAverage.Count > PingCount Then PingAverage.Dequeue(
Dim myEnum As IEnumerator = PingAverage.GetEnumerato
While myEnum.MoveNex
Sum = Sum + CLng(myEnum.Current
Console.WriteLine(myEnum.Current
End Whil
lblResult.Text = "Connection Speed: " & ret.ToString & " MS
Sum = Sum / PingAverage.Coun
Label2.Text = "Average for last " & PingAverage.Count & " pings: " & Sum & " MS
If Sum > pgPing.Maximum The
pgPing.Value = pgPing.Maximu
ElseIf Sum < 0 The
pgPing.Value =
Els
pgPing.Value = pgPing.Maximum - Su
End I
End Su
I hope someone can help me out. I have not had a lot of luck lately getting any help from this newsgroup. Kind of disappointing when you need the help
Brad Simon
What happens is that after the thread has been running awhile, it just stops. I cannot stop it, but when I restart the thread, it is fine. I don't mind restarting the thread, but I want it to do it automatically. Is there some code that I can put in to detect if the thread has gone awry?
I have a button on the form that simply starts and stops the thread. If I leave the thread running too long, the code stops to work. When that happens, I stop the thread, and I get the 'Thread cannot be stopped' message box (see code below). Then I can restart the thread
I used the code samples that the MSDN has to make it work. Here is the code I have now
Public CancelThread As New System.Threading.ManualResetEvent(False
Public ThreadisCanceled As New System.Threading.ManualResetEvent(False
Private Sub SomeLongTask(
' Run code until CancelThread is set
While Not CancelThread.WaitOne(0, False)
' Do some kind of task here
System.Threading.Thread.Sleep(CSng(txtPingDelay.Text) * 1000) 'txtpingdelay = .
ThreadPing(
End Whil
If CancelThread.WaitOne(0, False) The
'Acknowledge that the ManualResetEvent CancelThread is set
ThreadisCanceled.Set(
End I
End Su
Public Sub StartTask(
' Starts a new thread
Dim th As New System.Threading.Thread(AddressOf SomeLongTask
CancelThread.Reset(
ThreadisCanceled.Reset(
th.Start(
End Su
Public Sub CancelTask(
' Stops any threads started by the StartTask procedure
' Notice that this thread both receives and sends
' synchronization events to coordiante the threads.
CancelThread.Set() ' Set CancelThread to ask the thread to stop
If ThreadisCanceled.WaitOne(4000, False) The
' Wait up to 4 seconds for the thread to
' acknowledge that it has stopped
Els
MsgBox("The thread could not be stopped."
End I
End Su
Private Sub ThreadPing(
Dim Sum As Int32 =
Dim myPinger As IDOTPing.Ping = New IDOTPing.Pin
Dim ret As Int3
If txtHost.Text.Length < 1 The
ret = myPinger.PingHost("localhost"
Els
ret = myPinger.PingHost(txtHost.Text
End I
PingAverage.Enqueue(ret
If PingAverage.Count > PingCount Then PingAverage.Dequeue(
Dim myEnum As IEnumerator = PingAverage.GetEnumerato
While myEnum.MoveNex
Sum = Sum + CLng(myEnum.Current
Console.WriteLine(myEnum.Current
End Whil
lblResult.Text = "Connection Speed: " & ret.ToString & " MS
Sum = Sum / PingAverage.Coun
Label2.Text = "Average for last " & PingAverage.Count & " pings: " & Sum & " MS
If Sum > pgPing.Maximum The
pgPing.Value = pgPing.Maximu
ElseIf Sum < 0 The
pgPing.Value =
Els
pgPing.Value = pgPing.Maximum - Su
End I
End Su
I hope someone can help me out. I have not had a lot of luck lately getting any help from this newsgroup. Kind of disappointing when you need the help
Brad Simon