Krip said:
Then I'm afraid we will need to see some code to be able to help you.
CancelAsync should set worker.CancellationPending. I haven't tested if it
returns true in the GUI thread, but it should definitely return true inside
the worker thread.
The below code sample should demonstrate how cancellation should work. It
will run a background worker for ten seconds unless cancelled by clicking the
"Cancel" button.
public partial class Form1 : Form
{
BackgroundWorker worker = new BackgroundWorker();
public Form5()
{
InitializeComponent();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.WorkerSupportsCancellation = true;
Button bt = new Button();
bt.Text = "Cancel";
Controls.Add(bt);
bt.Click += new EventHandler(bt_Click);
worker.RunWorkerAsync();
}
void bt_Click(object sender, EventArgs e)
{
worker.CancelAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
DateTime start = DateTime.Now;
BackgroundWorker worker = sender as BackgroundWorker;
while ((DateTime.Now - start).TotalSeconds < 10)
{
if (worker.CancellationPending)
{
MessageBox.Show("Cancelled");
return;
}
Thread.Sleep(1000);
}
MessageBox.Show("Finished");
}
}