2

I got one page where i do some file manipulation, and when file is done, i need to upload to amazon s3. Sometimes file can be large, so user on submit need to wait too much. How can i make something like

  1. File manipulation
  2. When is done, i send file name parameters to some function
  3. I don't need to wait for that function, i want to use Response.Redirect before uploading is done.
Novkovski Stevo Bato
  • 1,013
  • 1
  • 23
  • 56

2 Answers2

9

Easiest way to do this:

ThreadPool.QueueUserWorkItem(YourUploadMethod);

There is some comment below arguing with this, so I wrote this:

    protected void Page_Load(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem(YourUploadMethod);

        Response.Redirect("http://google.com");
    }

    public void YourUploadMethod(object state)
    {
        Thread.Sleep(7000);
    }// breakpoint: I was redirected to google and then debugger stopped me here
Andriy Buday
  • 1,959
  • 1
  • 17
  • 40
  • 2
    I don't think this method will work for asp.net, because once the main thread finishes, the background threads die as well (regardless if they are finished or not). – hofnarwillie Apr 07 '12 at 18:20
  • 3
    I think Response.End, Response.Redirect, or Server.Transfer will only abort main thread. See this question: http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful – Andriy Buday Apr 07 '12 at 18:47
1

You need to start the method on a different thread - since you don't have to wait for the function to return, you don't need a callback.

See the Thread class and the Task class.

Oded
  • 489,969
  • 99
  • 883
  • 1,009