2

we have a scenario where in whil serving one asp.net request from iis from our code we have created a child thread from thread pool to servve some background task.the idea was to finsih the main thread wihich is processing the request without depending on our child thread task. But our doubt is while processing a request in asp.net will workerprocess wiat untill the thread finishes its task ?

Gonzalo
  • 20,805
  • 3
  • 75
  • 78

2 Answers2

4

No, if you've created a separate thread and don't have any code to wait on it, the request will certainly complete.

What I don't know is whether a worker process can be recycled while there are non-ASP.NET thread pool threads executing. I strongly suspect it can - so be aware that your child task could be terminated at any point. If this is a problem, you may wish to create a non-threadpool foreground thread.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • we are using asp.net mvc will there be any difference in handling thread in asp.net mvc? – Questionevrything Nov 11 '09 at 07:20
  • 1
    No. MVC runs "inside" ASP.NET. – Gonzalo Nov 11 '09 at 07:33
  • The worker process can indeed be terminated, depending on IIS settings. I once was on a project that ran long running batch jobs as separate threads in the ASP.NET application, so I know ;) (not my design) – Pete Nov 11 '09 at 08:06
  • Jon - ( additional info) if I understand you correctly- you're saying that the request will complete normally - Also any created threads ( via that request) will continue(!) executing.... well I agree - [I tested it this morning](http://i.stack.imgur.com/IrMij.jpg) - when page loads I register 3 tasks and send the client immediatly to another page [with the `endResponse` switch (default true)](http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.110).aspx)... – Royi Namir Jun 24 '14 at 08:24
  • @RoyiNamir: Yes, they'll continue executing - I'm not sure why that deserves an exclamataion mark, as they're just separate threads... – Jon Skeet Jun 24 '14 at 08:26
  • @JonSkeet because It does seems (not anymore) logic that if a request has opened a new thread and that request has ended ( due to response) - then - child threads should be ended. ( I know it's not the case - but that's what I thought). p.s. there are many answers here at SO that says - never to open background thread - because ASP will terminate them when the request has ended.... - hence the confusion . ( unless async -await is used) – Royi Namir Jun 24 '14 at 08:29
  • @RoyiNamir: I haven't seen such answers... there may be ones saying that any such threads may be terminated by ASP.NET at any time, but that's a different matter. – Jon Skeet Jun 24 '14 at 08:30
  • @JonSkeet [A sample of such answer](http://stackoverflow.com/questions/23483091/why-asp-net-kills-my-background-thread) (incorrect one). - [my remarks](http://i.stack.imgur.com/2ZPP0.jpg) – Royi Namir Jun 24 '14 at 08:42
  • 1
    @RoyiNamir: Odd. I can't explain the behaviour in that question... but I've definitely seen background threads *not* getting aborted... – Jon Skeet Jun 24 '14 at 08:44
3

No, the background thread will not wait for the main thread to finish, it will start immediately. If you use the Thread pool your task may have to wait for a thread to be available, but that is all.

You should however be aware that this may not be a good solution, for the application domain your asp.net site is running in may be recycled while your background thread is running. This will kill the background thread leaving the task incomplete. If it is possible, you should separate the background task into a service application that runs in a separate process. This will ensure that the task isn't killed while running.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76