Seehyung Lee
Seehyung Lee

Reputation: 610

ASP.NET Processing in the background

Is there any way to download a file from URL to server in the background? When a form is submitted, the file uploading processed automatically in the background, so user doesn't need to wait for the file upload.

In Windows Application, there is BackgroundWorker class. but how about ASP.NET?

Upvotes: 0

Views: 1913

Answers (1)

Yaakov Ellis
Yaakov Ellis

Reputation: 41560

This is not the sort of thing that you want to run on the Response thread, since it will delay the response to the user until the file can be download and processed. It is more well-suited for a different thread running as a service on the computer (or an Azure Worker Role) that will perform these items for you.

However, if you really want (or have to) run it through IIS/ASP.net, and if you don't care about returning anything to the user, you can run all of your logic in a different thread or through a delegate invoked asynchronously. See this answer (quoting from there) for a sample on running the delegate asynchronously.

private delegate void DoStuff(); //delegate for the action

protected void Button1_Click(object sender, EventArgs e)
{
    //create the delegate
    DoStuff myAction = new DoStuff(DownloadStuff); 

    //invoke it asynchronously, control passes to next statement
    myAction.BeginInvoke(null, null);
}

private void DownloadStuff() { 
  // your functionality here - this can also go in a code library
}

See MSDN for more info on how to work with delegates (including using parameters to pass info).

Upvotes: 2

Related Questions