First of all, your ThreadFinished() method is being called from DoWork(), which is running on a secondary thread - this means that all of the code you have in ThreadFinished() should *not* be touching any UI controls. You should alter your code to use a BackgroundWorker instead of a basic Thread object:
Thread m_NewThread = new Thread(new ThreadStart(DoWork));
m_NewThread.Start();
BackgroundWorker m_Worker = new BackgroundWorker();
m_Worker.DoWork += new DoWorkEventHandler(DoWork);
m_Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ThreadFinished);
m_Worker.RunWorkerAsync();
void DoWork(object sender, DoWorkEventArgs e)
{
/* Do your processing here - but don't touch any objects that
* inherit from Control (DoWork executes on background thread) */
}
void ThreadFinished(object sender, RunWorkerCompletedEventArgs e)
{
/* Safe to mess with Controls now (RunWorkerCompleted executes on main thread) */
//Thread has finished so show finish form and close
frmFinished f = new frmFinished();
f.Show();
this.Close();
}
However, that is probably only part of your problem. I am betting that your code sample belongs to the main form of the application - the one passed to Application.Run() in program.cs. If that is true, then calling this.Close() will cause your main application form to close, which effectively terminates your application.
HTH
|