Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a function but don't wait to finish - ASP.NET

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.
like image 279
Novkovski Stevo Bato Avatar asked Dec 21 '25 11:12

Novkovski Stevo Bato


1 Answers

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
like image 200
Andriy Buday Avatar answered Dec 23 '25 23:12

Andriy Buday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!