3

I wrote a website using ASP.NET.

There is a thread in the one page of my website. During the thread running, what happens to thread when user closes the browser window? Does this thread stop being run?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
sae
  • 39
  • 1
  • 2
  • You can try it with a fake long running thread (Thread.Sleep(100000)) and few logs. Thread won't stop its execution (because it's asynchronous to http request). Of course it'll be affected by ASP.NET rules so it's not such great idea for a very long running sensible task. – Adriano Repetti Jan 27 '15 at 08:13
  • "there is a thread in the one page of my website" meaning.. you're creating a thread? The thread will run until it is finished. If you put an endless while loop in it, it will run 'forever' – Jeroen van Langen Jan 27 '15 at 08:14
  • http://stackoverflow.com/questions/4333152/how-to-detect-browser-close-at-server-side-in-asp-net Hint: google for "asp.net what happens when user closes browser" ;) – PTwr Jan 27 '15 at 08:15
  • .NET has no safe way to stop a thread executing arbitrary code. It will simply continue to run. – usr Jan 27 '15 at 09:23

3 Answers3

6

Your server doesn't really know what the client did.

If the client started a request, the server will process it and return a response in any case.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
3

I think the question you're asking is not the one you want answered.

The answer to the question "Does asp.net lifecycle continue if I close the browser in the middle of processing?" is "yes". So anything you start from an ASPX page or MVC controller keeps running, even if the client's browser is closed (note that if that is your question, this is a duplicate).

However, the answer to "Can I use threads to carry out long-running jobs on IIS?" is "You can, but you shouldn't, because the thread may be terminated at any time", which you probably don't want.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

Please check this interesting conversation.According to this It will not stop even you closed it. https://forums.asp.net/t/1186025.aspx?What+happens+to+background+process+thread+when+one+closes+the+borwser+window+

mzonerz
  • 1,220
  • 15
  • 22