Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Request is not available in this context

Request is not available in this context

Scheduled Pinned Locked Moved ASP.NET
questionhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ryno Burger
    wrote on last edited by
    #1

    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?

    D N 2 Replies Last reply
    0
    • R Ryno Burger

      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?

      D Offline
      D Offline
      dojohansen
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Ryno Burger

        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?

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        DON'T CROSS POST :mad::mad: You were given answers in the original post.


        only two letters away from being an asset

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups