You should create a public property on the main form which returns the background worker. Then in your progress form you should pass a reference to the main form in the constructor. In the cancel button event call the bgw's CancelAsync event, something like this
BackgroundWorker bgw;
public BackgroundWorker formWorker
{
get
{
return bgw;
}
}
public frmParent()
{
InitializeComponent();
bgw = new BackgroundWorker();
bgw.WorkerSupportsCancellation = true;
bgw.DoWork +=new DoWorkEventHandler(backgroundWorker_DoWork);
bgw.RunWorkerAsync();
}
private void bt_add_Click(object sender, EventArgs e)
{
frmProgress progress = new frmProgress(this);
progress.ShowDialog();
}
static void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker backgroundWorker = sender as BackgroundWorker;
if(backgroundWorker != null)
{
while(!backgroundWorker.CancellationPending)
{
}
if(backgroundWorker.CancellationPending)
{
MessageBox.Show("Cancelled");
e.Cancel = true;
}
}
}
In you progress form
frmParent myParent;
public frmProgress(frmParent myParent)
{
InitializeComponent();
this.myParent = myParent;
}
private void button1_Click(object sender, EventArgs e)
{
myParent.formWorker.CancelAsync();
}
Hope this helps