开发者

Is BackgroundWorker in Asp.net can still working while it is navigated to other page?

开发者 https://www.devze.com 2023-02-27 05:45 出处:网络
I am using Backgroundworker to do data processing on the file uploaded by user, so it is time consuming. On the mean time, the user want to navigate to the other page to do something else.

I am using Backgroundworker to do data processing on the file uploaded by user, so it is time consuming. On the mean time, the user want to navigate to the other page to do something else.

So my question is, is it possible the BackgroundWorker still continue to process the uploaded data while the开发者_开发问答 current page is navigated to the other page??

If yes, how? sample code provided for reference is appreciated!


The BackgroundWorker will be destroyed and its work will never complete. There is no way around this, well, not no way around this, but if you use the "way around this" you will be shunned by anyone who ever sees your code (it involves storing metadata in session and deserializing it on page load to continue processing).

Your only real option is to separate your application into multiple parts. One ASP.NET page allows for the upload and puts the file in a directory. You then have a Windows Service or some other process perform the transformations that you need. If you have the Windows Service report progress to a SQL database, you can have a "status" page where the user can check the status of their uploaded file.

If you have to do all of this within a single web page, then you have to do what applications like Facebook do: until everything is done, you can't leave the page.

I know this wasn't what you were looking for, but it's the reality of developing on the web.


You can put your data proccessing code in another page and call it Asynchronously from the page that handle the upload.

Pesodo Code:

Page_Load()
{
    UplaoadFileAndSaveSomeWher();

    Call Asynchronous("ProccessorPage.aspx");

    Response.Redirect("Go To Other Place");

...
...
...

}

for calling Asynchronously a webpage you could use something like this:

string url = "http://www.google.com"; 
WebClient client = new WebClient(); 
client.DownloadDataAsync(new Uri(url)); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号