Hi,
Can you please go through the below example.
Form2 myForm = new Form2();
bool bFormOpened = false;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker backgroundWorker = sender as BackgroundWorker;
if (backgroundWorker != null)
{
while (!backgroundWorker.CancellationPending)
{
if (!bFormOpened)
{
myForm.Show();
bFormOpened = true;
}
}
if (backgroundWorker.CancellationPending)
{
myForm.Dispose();
e.Cancel = true;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void button3_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
Also , make sure the below two property set as TRUE for backgroundWorker1
- WorkerReportsProgress
- WorkersupportsCancellation
Hope this helps this to you!
Best Regards
Muthuraja