views:

33

answers:

2

In my ASP.NET application, a while down the stack I call the following code:

Public Shared Sub Larma(ByVal personId As Integer)
    Dim thread As New System.Threading.Thread(New ParametrizedThreadStart(AddressOf Larma_Thread))
    thread.Start(personId)
End Sub

Private Shared Sub Larma_Thread(ByVal personId As Integer)
    StartaLarm(personId)
    Thread.Sleep(1000 * 30)
    StoppaLarm(personId)
End Sub

While this thread is running, the rest of the request is handled and a response is sent to the client. However, since I never call thread.Abort() or anything of the like, and I am very inexperienced with threading in ASP.NET, I am worried that I'm opening up for memory leaks or other threading problems.

What happens with the thread I start with the code above after Larma_Thread finishes running?

+1  A: 

After the thread's code finishes executing, the thread will be stopped and its resources reclaimed.

Justin Ethier
+1  A: 

The thread will be terminated once your work is done.

Note that the thread might also be terminated before your work has finished I'd IIS decides it needs to recycle the ASP.NET worker thread.

Franci Penov
That might explain why it does not always work in our sharp environment. Is there any way to tell IIS that this thread should under no circumstances be killed before it's completed?
Tomas Lycken
Nope. IIS does not know what's happening inside the ASP.NET process and as long as the process is not handling incoming requests or is not responsive IIS considers it a fair game and can recycle it at any time. Though, I should check if there's something new in .Net 4 that can control process recycling through the integrated pipeline.
Franci Penov
Unfortunately, I don't have the opportunity to use .NET 4 on this project (at least not yet), so I need to find a way to solve this in .NET 2.0 (possibly 3.5). See this question for follow-up: http://stackoverflow.com/questions/3052200/why-does-this-threading-approach-not-work
Tomas Lycken