I have a Windows Form application that involves two Forms. The child form is used to export data into CSV files, and uses a background worker to write the file. While this is occurring I have the form hidden. The parent form is still active while the background worker is running, so the user can exit the application, even when the background worker is writing files. On the parent form I have added a FormClosing event handler to prompt the user if a background worker is still running. The problem I am encountering is accessing the background worker in the parent form. Here is what I tried...
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
ExportForm eForm = new ExportForm(GridView, TableName, GridProgressBar, ProgressLabel);
if (eForm.PartWorker.IsBusy == true)
MessageBox.Show("Busy");
}
The problem will this is that it is creating a new instance of the Child Form, so the background worker will never have true for it's IsBusy attribute. How could I access this background worker in my parent form so I can check to see if this condition is true.
Here is the code for the PartWorker BackgroundWorker...
#region PartWorker Events
void PartWorker_DoWork(object sender, DoWorkEventArgs e)
{
GetSwitch();
int batchNum = 0;
bool done = false;
ProgressLabel.Visible = true;
while (!done)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
PartWorker.ReportProgress(i);
}
done = Export.ExportPartition(SaveFile, DataTable, 50000, batchNum++);
}
}
void PartWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Progress.Style = ProgressBarStyle.Blocks;
Progress.Value = e.ProgressPercentage;
//May want to put the file name that is being written here.
ProgressLabel.Text = "Writing File: " + e.ProgressPercentage.ToString() +"% Complete";
}
void PartWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Progress.Value = 100;
ProgressLabel.Visible = false;
Progress.Visible = false;
MessageBox.Show("Files sucessfully created!", "Files Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
PartWorker.Dispose();
this.Close();
}
#endregion