Request is not available in this context
-
Hi guys I have a function called SendEmail which runs within a thread. This function simply send an email to the user. However, I made a "Please Wait..." page while the email gets send in the background the user see the "Please Wait..." page.
public void SendEmail() { StringBuilder messageText = new StringBuilder(); messageText.AppendLine(string.Format("Name: {0} {1}", txtName.Text.Trim().ToString(), txtSurname.Text.Trim().ToString())); messageText.AppendLine(string.Format("Contact Number: {0}", txtContactNumber.Text.Trim().ToString())); messageText.AppendLine(string.Format("Email Address: {0}", txtEmail.Text.Trim().ToString())); messageText.AppendLine(string.Format("Comments: {0}\n\n", txtComments.Text.Trim().ToString())); messageText.AppendLine(string.Format("Quote Reference Nr: {0}", Request.QueryString["quoteNr"].ToString())); // Breaks here with error: Request is not available in this context // Send the email to the user }
I call the SendEmail() function using the following lines of code:Guid id = Guid.NewGuid(); ThreadStart ts = new ThreadStart(SendEmail); Thread th = new Thread(ts); th.Start(); Response.Redirect(string.Format("PleaseWait.aspx?ID={0}&type={1}", id, "email"));
Is it true that you can't access Request data within a thread, if not, how do I then get access to Request.QueryString data? -
Hi guys I have a function called SendEmail which runs within a thread. This function simply send an email to the user. However, I made a "Please Wait..." page while the email gets send in the background the user see the "Please Wait..." page.
public void SendEmail() { StringBuilder messageText = new StringBuilder(); messageText.AppendLine(string.Format("Name: {0} {1}", txtName.Text.Trim().ToString(), txtSurname.Text.Trim().ToString())); messageText.AppendLine(string.Format("Contact Number: {0}", txtContactNumber.Text.Trim().ToString())); messageText.AppendLine(string.Format("Email Address: {0}", txtEmail.Text.Trim().ToString())); messageText.AppendLine(string.Format("Comments: {0}\n\n", txtComments.Text.Trim().ToString())); messageText.AppendLine(string.Format("Quote Reference Nr: {0}", Request.QueryString["quoteNr"].ToString())); // Breaks here with error: Request is not available in this context // Send the email to the user }
I call the SendEmail() function using the following lines of code:Guid id = Guid.NewGuid(); ThreadStart ts = new ThreadStart(SendEmail); Thread th = new Thread(ts); th.Start(); Response.Redirect(string.Format("PleaseWait.aspx?ID={0}&type={1}", id, "email"));
Is it true that you can't access Request data within a thread, if not, how do I then get access to Request.QueryString data?Hi, all code runs within a thread or another :) the difference is just that you explicitly created the one from which you want to send mail. It makes no sense for the request to be available outside the "main" thread (the one created by asp.net rather than your code) since the asp.net model is to free all the resources used for a specific request once the response has been sent. It is possible to hack it by keeping your own references to it, but that would be bad to say the least, and not only for performance - you'll never know if the state of the object is good or not, it might be disposed or even reused in a pool for all we know, today or in a future version of ASP.NET. However, all your method neeeds is to know the quote reference number, so the solution is to make it get this from somewhere other than the request. If you're using at least v2.0 you can use the parameterized thread start delegate and you're done. In 1.x a somewhat more sophisticated solution must be invented, such as using a synchronized ArrayList. All of this having been said, because I'm such a nice bloke and want you to understand the problem and how one can solve it, I have to say that it is utterly nonsensical to make a new thread for what your sendmessage method is doing. It is not only needlessly complicated but the overhead of creating and starting the thread is huge compared to the amount of work performed in the thread, so its a performance killer to boot. Right here therefore, the solution is simply to get rid of your multithreaded design where a singlethreaded one is far better.
-
Hi guys I have a function called SendEmail which runs within a thread. This function simply send an email to the user. However, I made a "Please Wait..." page while the email gets send in the background the user see the "Please Wait..." page.
public void SendEmail() { StringBuilder messageText = new StringBuilder(); messageText.AppendLine(string.Format("Name: {0} {1}", txtName.Text.Trim().ToString(), txtSurname.Text.Trim().ToString())); messageText.AppendLine(string.Format("Contact Number: {0}", txtContactNumber.Text.Trim().ToString())); messageText.AppendLine(string.Format("Email Address: {0}", txtEmail.Text.Trim().ToString())); messageText.AppendLine(string.Format("Comments: {0}\n\n", txtComments.Text.Trim().ToString())); messageText.AppendLine(string.Format("Quote Reference Nr: {0}", Request.QueryString["quoteNr"].ToString())); // Breaks here with error: Request is not available in this context // Send the email to the user }
I call the SendEmail() function using the following lines of code:Guid id = Guid.NewGuid(); ThreadStart ts = new ThreadStart(SendEmail); Thread th = new Thread(ts); th.Start(); Response.Redirect(string.Format("PleaseWait.aspx?ID={0}&type={1}", id, "email"));
Is it true that you can't access Request data within a thread, if not, how do I then get access to Request.QueryString data?DON'T CROSS POST :mad::mad: You were given answers in the original post.
only two letters away from being an asset