I am developing an ASP.Net Website, using C# language, ASP.Net Website Project.
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 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.
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(() => 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 > 0)
{
lblMessage.Text = errMessage;
return;
}
}
Now I have three questions.
1. Is the above code looks fine to execute new thread with parameters? If not, please do let me know your suggestions
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?
3. So the background process once generated, will always be completed even though browser gets closed?
niravparekh
Member
704 Points
427 Posts
What happens if new Thread has started in background and web page gets closed?
Apr 25, 2012 06:09 PM|LINK
Hi,
I am developing an ASP.Net Website, using C# language, ASP.Net Website Project.
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 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.
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(() => 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 > 0) { lblMessage.Text = errMessage; return; } }Now I have three questions.
1. Is the above code looks fine to execute new thread with parameters? If not, please do let me know your suggestions
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?
3. So the background process once generated, will always be completed even though browser gets closed?
Thanks in advance,