What happens if new Thread has started in background and web page gets closed?http://forums.asp.net/t/1796940.aspx/1?What+happens+if+new+Thread+has+started+in+background+and+web+page+gets+closed+Wed, 25 Apr 2012 18:09:55 -040017969404951414http://forums.asp.net/p/1796940/4951414.aspx/1?What+happens+if+new+Thread+has+started+in+background+and+web+page+gets+closed+What happens if new Thread has started in background and web page gets closed? <p>Hi,</p> <p>I am developing an ASP.Net Website, using C# language, ASP.Net Website Project.</p> <p>On one of my button click, After calling some function to update database values, I have a function which generates .pdf file out of my crystal report and send it as attachment with email. Now this function takes little long about&nbsp;7 seconds so what I did is, I started a new thread in background and called this function in new thread. Here is the code for that.</p> <pre class="prettyprint">public class Parameters_For_Thread { public int RequestInfoID = 0; public HttpContext MyContext; } public void btnApproveRequest_Click(object sender, EventArgs e) { //I call some functions here to update some database values // I call my function to refresh Grid data on page // Now need to call function which takes little long, this function generates .pdf file out of crystal report and send email to user with that attachment Parameters_For_Thread obj1 = new Parameters_For_Thread(); obj1.MyContext = HttpContext.Current; obj1.RequestInfoID = RequestInfoID; var t = new Thread(() =&gt; MyTest(obj1)); t.IsBackground = true; t.Start(); } public void MyTest(Parameters_For_Thread obj1) { // This is my function which takes long time so I want to execute in background to generate .pdf file and to send email if (errMessage.Length &gt; 0) { lblMessage.Text = errMessage; return; } }</pre> <p>Now I have&nbsp;three questions.</p> <p>1. Is the above code looks fine to execute new thread with parameters? If not, please do let me know your suggestions</p> <p>2. What happens if user clicks on button, and when it reaches to execute that code where I am creating new thread, and user closes that web page or browser or he moves away from that page? Will the process complete in any case?</p> <p>3. So the background process once generated, will always be completed even though browser gets closed?</p> <p>Thanks in advance,</p> 2012-04-25T18:09:55-04:00