1

I'm working with C# ASP.NET Web Application. There is a problem that is difficult to track in the code. Let's say I have two buttons: Upload (which uploads some data from DataTable to Database) and Cancel (which cancels the uploading process). The problem is that uploading process runs in the BackgroundWorker. When user clicks Upload and uploading starts and then he clicks Cancel the Cancel_Button_Click event does not fire until BackgroundWorker is uploading data.

I have code like this:

protected void Page_Load(object sender, EventArgs e)
{
    backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
}

protected void Upload_Button_Click(object sender, EventArgs e)
{ backgroundWorker1.RunWorkerAsync(); }

protected void Cancel_Button_Click(object sender, EventArgs e)
{ backgroundWorker1.CancelAsync(); }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{ };

So what really happens is when you click Upload and Cancel is method private void backgroundWorker1_ProgressChanged runs many times (one time for every row of data, I suppose) and ONLY THEN method Cancel_Button_Click runs.

What to do, if I want to stop data uploading right after Cancel button is clicked?

Thaaaanks! :)

P.S. I tried searching on the net but I don't even know how to describe my problem in one sentence.

QwwQ
  • 21
  • 3
  • possible duplicate of [How to stop BackgroundWorker correctly](http://stackoverflow.com/questions/4732737/how-to-stop-backgroundworker-correctly) – cdsln Jul 24 '15 at 13:27
  • Thanks for you answer. :) Pretty different problems. :( – QwwQ Jul 24 '15 at 13:58

0 Answers0