We help IT Professionals succeed at work.
Get Started

How to close application form from another Thread in C# .NET?

Last Modified: 2013-12-17
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;

                //Instance Thread object with ThreadStart object.
                _threadA = new Thread(_threadStartA);
                //Name the Thread
                _threadA.Name = "Phase1";
                //Spawn the Thread
            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.RecoverCommandStarted(oRecovery_OnCmdStarted);
            oRecovery.OnCmdExited += new Test.com.Recovery.RecoverCommandExited(oRecovery_OnCmdExited);
            oRecovery.OnCmdOutputLine += new Test.com.Recovery.RecoveryCommandOutputLine(oRecovery_OnCmdOutputLine);

            //Hook image events
            oRecovery.OnStarted += new Test.com.Recovery.RecoverStartedEventHandler(oRecovery_OnStarted);
            oRecovery.OnComplete += new Test.com.Recovery.RecoverCompleteEventHandler(oRecovery_OnComplete);
            oRecovery.OnProgress += new Test.com.Recovery.RecoverProgressEventHandler(oRecovery_OnProgress);
            oRecovery.OnProcess += new Test.com.Recovery.RecoverProcessEventHandler(oRecovery_OnProcess);
            oRecovery.OnError += new Test.com.Recovery.RecoverErrorEventHandler(oRecovery_OnError);

            //First we need to get a DiskPart object
            if (oRecovery.ConfigureDisk(PopulateDiskPart(), true))
                if (oRecovery.RecoverImage(PopulateImage(ImageType.Debug)))//Recover 1st image first
                    if (oRecovery.RecoverImage(PopulateImage(ImageType.Debug)))//Recover 2nd image last
                                         this.Hide() <--- BAD wont work cross thread exception.
                        ***** DONE CLOSE THIS FORM AND OPEN A NEW FORM
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE