yami_rider
asked on
How to close application form from another Thread in C# .NET?
I have two forms, I am running some worker events on a thread in form A, when that work is done I want to close Form A and show Form B.
If I simply use this.hide() I receive a cross thread exception which makes sense because I am not accessing it from the same thread. Is this even possible?
private void CreateWorkerThread()
{
//Instance a ThreadStart object and intance it with the SnapShot class CollectData method.
ThreadStart _threadStartA = new ThreadStart(DoWork);
//Initialize a Thread object and set to null;
Thread _threadA = null;
try
{
//Instance Thread object with ThreadStart object.
_threadA = new Thread(_threadStartA);
//Name the Thread
_threadA.Name = "Phase1";
//Spawn the Thread
_threadA.Start();
}
catch (Exception exp)
{
MessageBox.Show("Image Apply error: " + exp.Message);
}
}
private void DoWork()
{
Test.com.Recovery.Recovery oRecovery = new Test.com.Recovery.Recovery ();
//Hook command line events
oRecovery.OnCmdStarted += new Test.com.Recovery.RecoverC ommandStar ted(oRecov ery_OnCmdS tarted);
oRecovery.OnCmdExited += new Test.com.Recovery.RecoverC ommandExit ed(oRecove ry_OnCmdEx ited);
oRecovery.OnCmdOutputLine += new Test.com.Recovery.Recovery CommandOut putLine(oR ecovery_On CmdOutputL ine);
//Hook image events
oRecovery.OnStarted += new Test.com.Recovery.RecoverS tartedEven tHandler(o Recovery_O nStarted);
oRecovery.OnComplete += new Test.com.Recovery.RecoverC ompleteEve ntHandler( oRecovery_ OnComplete );
oRecovery.OnProgress += new Test.com.Recovery.RecoverP rogressEve ntHandler( oRecovery_ OnProgress );
oRecovery.OnProcess += new Test.com.Recovery.RecoverP rocessEven tHandler(o Recovery_O nProcess);
oRecovery.OnError += new Test.com.Recovery.RecoverE rrorEventH andler(oRe covery_OnE rror);
//First we need to get a DiskPart object
if (oRecovery.ConfigureDisk(P opulateDis kPart(), true))
if (oRecovery.RecoverImage(Po pulateImag e(ImageTyp e.Debug))) //Recover 1st image first
if (oRecovery.RecoverImage(Po pulateImag e(ImageTyp e.Debug))) //Recover 2nd image last
{
this.Hide() <--- BAD wont work cross thread exception.
***** DONE CLOSE THIS FORM AND OPEN A NEW FORM
}
}
If I simply use this.hide() I receive a cross thread exception which makes sense because I am not accessing it from the same thread. Is this even possible?
private void CreateWorkerThread()
{
//Instance a ThreadStart object and intance it with the SnapShot class CollectData method.
ThreadStart _threadStartA = new ThreadStart(DoWork);
//Initialize a Thread object and set to null;
Thread _threadA = null;
try
{
//Instance Thread object with ThreadStart object.
_threadA = new Thread(_threadStartA);
//Name the Thread
_threadA.Name = "Phase1";
//Spawn the Thread
_threadA.Start();
}
catch (Exception exp)
{
MessageBox.Show("Image Apply error: " + exp.Message);
}
}
private void DoWork()
{
Test.com.Recovery.Recovery
//Hook command line events
oRecovery.OnCmdStarted += new Test.com.Recovery.RecoverC
oRecovery.OnCmdExited += new Test.com.Recovery.RecoverC
oRecovery.OnCmdOutputLine += new Test.com.Recovery.Recovery
//Hook image events
oRecovery.OnStarted += new Test.com.Recovery.RecoverS
oRecovery.OnComplete += new Test.com.Recovery.RecoverC
oRecovery.OnProgress += new Test.com.Recovery.RecoverP
oRecovery.OnProcess += new Test.com.Recovery.RecoverP
oRecovery.OnError += new Test.com.Recovery.RecoverE
//First we need to get a DiskPart object
if (oRecovery.ConfigureDisk(P
if (oRecovery.RecoverImage(Po
if (oRecovery.RecoverImage(Po
{
this.Hide() <--- BAD wont work cross thread exception.
***** DONE CLOSE THIS FORM AND OPEN A NEW FORM
}
}
ASKER
Maybe I should go into more detail about this. Actually I am creating an application with several forms.
1. 3 Forms
a. First form is a end user license agreement with two buttons (agree and cancel)
b. Second form is will be displayed to the user showing the recovery process. This has a progress bar.
c. Last form is simply a form that tells the user the recovery is complete or not complete.
If you can think of a setup application where the user is brought through multiple forms that is what I am trying to achieve.
When the EULA is displayed and the user clicks on agree I want the EULA form to disappear and the
Second form to appear and begin the recovery. When the recovery completes, I want the second form to close and the final form displaying that the recovery is complete or not complete to appear.
In the second form since I have controls on it and I am doing some background operations I spawn a thread and safely invoke the update on the controls (i.e. progress bar, etc.). This is where I think my problem is.
1. 3 Forms
a. First form is a end user license agreement with two buttons (agree and cancel)
b. Second form is will be displayed to the user showing the recovery process. This has a progress bar.
c. Last form is simply a form that tells the user the recovery is complete or not complete.
If you can think of a setup application where the user is brought through multiple forms that is what I am trying to achieve.
When the EULA is displayed and the user clicks on agree I want the EULA form to disappear and the
Second form to appear and begin the recovery. When the recovery completes, I want the second form to close and the final form displaying that the recovery is complete or not complete to appear.
In the second form since I have controls on it and I am doing some background operations I spawn a thread and safely invoke the update on the controls (i.e. progress bar, etc.). This is where I think my problem is.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what owns the instance of FormA?