Thread issue/leak?

C

Chris Botha

I am creating a worker thread from inside an aspx page and the thread does
the stuff it should do, no problem.
However, I have noticed running it in the debugger that it seems as if the
threads are not killed/garbage-collected after a thread terminates. I have a
breakpoint in the thread itself and every time it breaks, in the Threads
window I observe the new thread, but the previous thread is still there as
well. The exact same code in a windows app performs as expected, on every
break there are two threads, the main thread and the current worker thread.
The simplified code below also gives the same results, so nothing strange in
the thread itself. If I put the code in a loop of 2000 cycles and break
after the last, then there are 2000 worker threads displayed in the Threads
window.

Dim ttt As Thread = New Thread(AddressOf TheThreadProc)
ttt.ApartmentState = Threading.ApartmentState.STA
ttt.Start()
ttt.Join()

And the simple thread:

Private Sub TheThreadProc()
Dim iii As Integer = 10
End Sub
 
K

Kevin Spencer

This is because you are using a Single-Threaded Apartment. There is nothing
in the thread to receive messages. When you use COM in an ASP.Net
application, you create an Interop class that is disposable, and therefore,
the thread can be killed. IOW, don't use an STA thread unless you are doing
Interop.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
C

Chris Botha

Kevin, thanks for the response, however this is not it, I tested it without
STA before I posted the problem, same result.
The true thread is doing Interop, the simple example below is not (the
simple example produces the error/issue, with or without the STA statement).
 
K

Kevin Spencer

Are you setting the "aspcompat" attribute?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
K

Kevin Spencer

Also, Chris, as you didn't post your actual code, I can't really guess what
the problem is. However, I believe you can find your answers at one or more
of the following MSDN articles (and related links):

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmemorymanagement.asp?frame=true
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinteropmarshaling.asp?frame=true

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
H

hB

worker threads should be terminated, as you showed code of one
statement.
There would be 2000 (caller) main threads waiting for worker to be
terminated.

If you are saying worker thread (small one statement proc) is not
terminating (GC)
then Caller threads must also be alive.

Check ttt.IsAlive and do ttt.Abort() and ttt = Nothing
and test.

The (worker thread) code is being called by pageclass which is also
generated by some other asp.net parent thread (if that is calling abort
or someother stuff on the thread of pageclass this could affect).

Do let us know of your proceedings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top