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

    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

    Any ideas of what I am missing ?
    Guest, Nov 14, 2004
    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

    Make a Progress Indicator For Slow-Loading Pages
    DESIGN PATTERNS: Asynchronous Wait State Pattern in ASP.NET

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

  3. Hey Robert.
    The reason why you aren't getting what you want is because ot the
    Even if you called Response.Redirect the runtime MUST end the current
    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
  4. Guest

    Guest Guest


    Guest, Nov 15, 2004
    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.