G
Guest
Hi,
i have the problem that when i create a low priority background thread,
start it, and wait for it to finish that it does not seem to terminate even
after it is finished..
if i use performance monitoring to watch the actual thread count then it
never goes down.. everytime i create a thread it goes up by one.. even if i
abort the thread and set the thread object = nothing it still doesn't decrease
i am now having a problem with one applpication that creates and closes 2
threads every hour that it starts using 50% cpu after a few days even while
doing absolutly NOTHING.. i think that this might be due to the thread
overhead of a few thousend unfinished threads...
so how to i properly terminate a worker thread?!
..abort doesn't work
=nothing doesn't work
knowing the thread is supposed to be finished (has finished all tasks,
thread start sub has exited! but yet it sill doesn't close!!!!
here is the a sample code to show what i mean:
(simple console program, in an infinite loop, it creates a new thread every
3 seconds.. every second it shows the number of threads it is using)
Module Module1
Sub Main()
Dim oDiag As New System.Diagnostics.PerformanceCounter(".NET CLR
LocksAndThreads", "# of current recognized threads",
IO.Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess.MainModule.FileName), My.Computer.Name)
Dim iCount As Integer = 1, oThread As Threading.Thread = Nothing
Do
Console.WriteLine("Thread Count: " & oDiag.RawValue)
iCount += 1
If (iCount > 3) Then
iCount = 1
If oThread IsNot Nothing Then oThread.Abort()
oThread = New Threading.Thread(AddressOf ThreadStart)
oThread.Priority = Threading.ThreadPriority.BelowNormal
oThread.IsBackground = True
oThread.Start()
'Threading.ThreadPool.QueueUserWorkItem(AddressOf ThreadStart)
End If
Threading.Thread.Sleep(1000)
Loop
End Sub
Private Sub ThreadStart(Optional ByVal oState As Object = Nothing)
Console.WriteLine("Thread Started!")
For i As Integer = 1 To 1000
Dim a As Double = (i * 3.73)
a = i
Next
Console.WriteLine("Thread Finished! (Should Terminate, Right?)")
End Sub
End Module
please, any help would be greatly appriciated!
i have the problem that when i create a low priority background thread,
start it, and wait for it to finish that it does not seem to terminate even
after it is finished..
if i use performance monitoring to watch the actual thread count then it
never goes down.. everytime i create a thread it goes up by one.. even if i
abort the thread and set the thread object = nothing it still doesn't decrease
i am now having a problem with one applpication that creates and closes 2
threads every hour that it starts using 50% cpu after a few days even while
doing absolutly NOTHING.. i think that this might be due to the thread
overhead of a few thousend unfinished threads...
so how to i properly terminate a worker thread?!
..abort doesn't work
=nothing doesn't work
knowing the thread is supposed to be finished (has finished all tasks,
thread start sub has exited! but yet it sill doesn't close!!!!
here is the a sample code to show what i mean:
(simple console program, in an infinite loop, it creates a new thread every
3 seconds.. every second it shows the number of threads it is using)
Module Module1
Sub Main()
Dim oDiag As New System.Diagnostics.PerformanceCounter(".NET CLR
LocksAndThreads", "# of current recognized threads",
IO.Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess.MainModule.FileName), My.Computer.Name)
Dim iCount As Integer = 1, oThread As Threading.Thread = Nothing
Do
Console.WriteLine("Thread Count: " & oDiag.RawValue)
iCount += 1
If (iCount > 3) Then
iCount = 1
If oThread IsNot Nothing Then oThread.Abort()
oThread = New Threading.Thread(AddressOf ThreadStart)
oThread.Priority = Threading.ThreadPriority.BelowNormal
oThread.IsBackground = True
oThread.Start()
'Threading.ThreadPool.QueueUserWorkItem(AddressOf ThreadStart)
End If
Threading.Thread.Sleep(1000)
Loop
End Sub
Private Sub ThreadStart(Optional ByVal oState As Object = Nothing)
Console.WriteLine("Thread Started!")
For i As Integer = 1 To 1000
Dim a As Double = (i * 3.73)
a = i
Next
Console.WriteLine("Thread Finished! (Should Terminate, Right?)")
End Sub
End Module
please, any help would be greatly appriciated!