473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Threads do not Terminate!

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.Diagnos tics.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!

Jun 29 '06 #1
2 1672
A few more things i found out:

it only does this in .Net 2.0 (2005)

i modified the same program to work with .Net 1.1 (2003) (see source here
below)

when a thread finishes in .Net 1.1, it FINISHES!.. in .Net 2.0 it does
something ELSE, i mean other then finishing... WHY?! A Thread doesn't use
iDisposable, i can't make sure it 'disposed' of.. i can't make sure it
stops...

is this a bug with .Net 2.0?... or did they change the thread handing in
such a way that whatever i have been doing (and was working) is now deemed to
be horribly worng?

here the same code fixed up for .Net 1.1:

Module Module1
Sub Main()
Dim oDiag As New System.Diagnostics.PerformanceCounter(".NET CLR
LocksAndThreads", "# of current recognized threads",
IO.Path.GetFileNameWithoutExtension(System.Diagnos tics.Process.GetCurrentProcess.MainModule.FileName ), System.Net.Dns.GetHostName())
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 Not oThread Is Nothing Then oThread.Abort()
oThread = New Threading.Thread(AddressOf ThreadStart)
oThread.Priority = Threading.ThreadPriority.BelowNormal
oThread.IsBackground = True
oThread.Start()
End If
Threading.Thread.Sleep(1000)
Loop
End Sub
Private Sub ThreadStart()
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

Jun 29 '06 #2
Hello R. Nachtsturm,
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..
I think the problem you're running into here is the difference between native
and managed threads. Whenever you have a thread in VB (or any managed language
for that matter) it is backed by a real operating system thread. There is
not a strict 1 to 1 relationship here and the CLR often keeps a lot of native
threads lying around to make managed threads faster.
Console.WriteLine("Thread Count: " & oDiag.RawValue)
I had to alter your sample slightly here because I wasn't able to create
the appropriate performance counter. I changed the above line to the following
(pretty much equivalent).
Console.WriteLine("Thread Count: " & Process.GetCurrentProcess().Threads.Count)


For me in constantly prints out that there are 11 threads in the process.
This is actually the count of native operating system threads. However
the managed threads are finishing. You can verify this by adding a oThread.Join()
immediately after oThread.Start(). It still says 11 threads but it is able
to join successfully.

--
Jared Parsons [MSFT]
ja******@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jun 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ralph Sluiters | last post by:
Hi, i've got a small problem with my python-script. It is a cgi-script, which is called regulary (e.g. every 5 minutes) and returns a xml-data-structure. This script calls a very slow function,...
4
by: Gabriele Bartolini | last post by:
Hi, I am writing an application in C++ on Linux, using threads (this is my first experience with pthreads). The application itself is fine, it is just that I wanted to handle asynchronous...
10
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
4
by: Senthil | last post by:
Hi.. Can anyone tell how to find the threads(name or ID) running in the background using C#. I want to find those threads and to terminate if needed. Thanks, Palani.
1
by: andreas.baus | last post by:
Hi. I'm trying to figure out how to properly start and stop threads from a windows form. One thing I still have a problem with is how to make sure the spawned thread(s) are shut down when the...
1
by: LordHog | last post by:
Hello all, I am a small application that I am developing which will use a few threads. When the user closes the form the threads will remain present until I close them. The Main Form...
4
by: MSDousti | last post by:
Hi I have written a VB .NET app, which uses several threads. I thought that when the user closes the main window (when MainForm.closed event occures, and I call application.exit) all running...
3
by: Abubakar | last post by:
Hi, from msdn: "Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread." Through the...
2
by: Steve Holden | last post by:
Jonathan Shao wrote: Yes - you are calling it before you have started ALL your threads, thereby making hte main thread wait for the end of thread 1 before starting the next. An impressive...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.