i am calling an asmx service from my windows form application in a background worker. i want to be able to cancel/stop the call once a button is pressed.
i am currently performing the following:
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
GlobalVariables.worker = sender as BackgroundWorker;
try
{
if (GlobalVariables.worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
else
{
e.Result = CallServer(ServerName, ActionName); // call on a web method in the referenced service
}
}
catch (Exception ee)
{
GlobalFunctions.ShowError(ee);
}
}
private void button1_Click(object sender, EventArgs e) //button to cancel the call
{
if (GlobalVariables.worker.IsBusy == true)
{
//GlobalVariables.worker.thre;
GlobalVariables.worker.CancelAsync();
GlobalVariables.worker.Dispose();
}
//GlobalVariables.worker.CancelAsync();
Form1.f.Enabled = true;
Form1.f.progressBar1.Text = "Done!";
Form1.f.progressBar1.Visible = false;
Form1.f.textBox1.Visible = true;
Close();
}
the following doesnt seem to work because when i press on cancel, the web method called in the service reference is not stopped and still returns data