i have a problem with my code. Im creating a graphic app that have a main form. When i click on a button i create a new form that show a progress bar while export some files. My problem start when i try to close the form with the progress bar because method what export files doesnt end.
In the new form, i execute a method that check the progress of the export and fill the progress bar, this method is executed each second. To export i create a new thread, and this thread execute the export method.
If the export finish, form and thread close right way but if i "force" stop operation by closing the form, the thread that are exporting doesnt stop until the export ends or when i close the main form.
So, how can i stop that thread?
This is my code:
public Form3(File file, string output, string inputFile)
{
InitializeComponent();
this.file = file;
this.output = output;
this.inputFile = inputFile;
progressLabel.Location = new Point(textProgress.Right, progressLabel.Top);
thread = new Thread((ThreadStart)delegate { Exporter.ExportToFile(this.file, this.output, this.inputFile); });
thread.IsBackground = true;
thread.Start();
TimerControl();
}
private void TimerControl()
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Tick += new EventHandler(GetProgress);
t.Interval = 1000; // in miliseconds
t.Start();
}
private void GetProgress(object sender, EventArgs myEventArgs)
{
int x = Exporter.GetProgress();
progressBar.Maximum = 100;
if (!Exporter.stop)
{
progressBar.Value = x;
progressLabel.Text = x.ToString() + "%";
}
else
{
this.Close();
}
}