I have an aspx page that on a button click creates a BackgroundWorker and then calls the RunWorkerAsync method. In my aspx file I have set Async='true' but when I run the application and click the button it appears as though the page waits to refresh until the processing of the BackgroundWorker is done. How do I get control to return to the page (and have the page refresh) after the BackgroundWorker is created and started?
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += RunJob;
worker.RunWorkerCompleted += JobCompleted;
worker.RunWorkerAsync();
Asynchronous pages are great, but they don’t do what you are thinking. You need to actually complete the request in order for the page to render. Then you can issue another (asynchronous JavaScript) request to the server, polling for completion.
Using a
BackgroundWorkeris not going to work for this. You’ll need to kick off a background task (or thread, if application recycling isn’t an issue), and track completion of those operations yourself. Then, when the “is it done yet” request comes in, reply with a “not yet” message or the results.