redirect and thread execution

Discussion in 'ASP .Net' started by Guest, Nov 14, 2004.

  1. Guest

    Guest Guest

    I have a problem with thread execution after calling response.redirect() in a
    web form.

    Basically, I want the thread to continue to work (e.g. do some long database
    work) but want to redirect immediately to another page, so that the user does
    not have to wait.

    I tried the wollowing exemple with two webforms webform1.aspx and
    webform2.aspx : WebForm1.aspx just has one button which, when clicked,
    executes the following method

    private void Button1_Click(object sender, System.EventArgs e)
    {
    Server.ScriptTimeout = 60; // One minute
    Response.Redirect("WebForm2.aspx",false);
    Response.Flush();
    Response.Close();

    Thread.Sleep(10000); // Simulate the work to do
    }

    When WebForm1 Button1 is clicked, the false option in the Response.Redirect
    method says that thread execution should continue. Then, as Response.Flush()
    and Response.Close() are called, the Redirect should take place immediatly.

    However, when I try (under IIS6) the browser waits for the Sleep command to
    finish and only redirects afterwards. This does not seem to be the correct
    behavior.

    Any ideas of what I am missing ?
    Robert
     
    Guest, Nov 14, 2004
    #1
    1. Advertisements

  2. For one thing, you don't want to tie up an ASP.NET thread with database
    work. The thread will not be available for other requests. Instead, queue
    work like this to a worker process which is outside of the ASP.NET
    application (perhaps in a Windows Service).

    Look at the following for ways to do what you're trying to do.
    Indicating Progress
    http://msdn.microsoft.com/library/d...serverprogressfromaspnetclientapplication.asp

    Make a Progress Indicator For Slow-Loading Pages
    (http://www.aspnetpro.com/NewsletterArticle/2003/08/asp200308bm_l/asp200308bm_l.asp)
    DESIGN PATTERNS: Asynchronous Wait State Pattern in ASP.NET
    http://msdn.microsoft.com/msdnmag/issues/03/12/designpatterns/default.aspx

    John Saunders
     
    John Saunders, Nov 14, 2004
    #2
    1. Advertisements

  3. Hey Robert.
    The reason why you aren't getting what you want is because ot the
    Thread.Sleep().
    Even if you called Response.Redirect the runtime MUST end the current
    function.
    Only after that Response.Flush will take effect. And because Thread.Sleep
    freezes the current thread you end up waiting for it to finish.
    Kirill Osipov
     
    Kirill Osipov, Nov 14, 2004
    #3
  4. Guest

    Guest Guest

    Thanks!

     
    Guest, Nov 15, 2004
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.