Link to home
Start Free TrialLog in
Avatar of GRChandrashekar
GRChandrashekarFlag for India

asked on

Closing Thread

In below code, the code continues to execute inspite of application.exit and gives error Invoke or BeginInvoke cannot be called on a control until the window handle has been created.. How do I make sure application exits where the code is written for exit ?
 private void ClubCentricUpdater_Load(object sender, EventArgs e)
        {
            flashLabel.Text = @"Downloading Updates. Please Wait";
            lblsupport.Text = @"Problems, if any, please contact IT Support";
            ThreadStart ts = delegate
                                 {
                                     try
                                     {
                                         bool version = FileCompare(Comparesourcefile,
                                                                    Comparedestinationfile);
                                         if (version)
                                         {
                                             //Exit this application and open the desired application
                                             var rundesiredprocess = new Process();
                                             rundesiredprocess.StartInfo.FileName =
                                                 desiredapplication;
                                             rundesiredprocess.Start();
                                             
                                             CloseForm(this);
                                             Application.Exit();
                                         }
                                         FindAndKillProcess("notepad");
                                         filePaths = Directory.GetFiles(sourcepath, "*.*",
                                                                        SearchOption.AllDirectories);
                                         Invoke(new MethodInvoker(delegate
                                                                      {
                                                                          progressBar.Maximum =
                                                                              filePaths.Length;
                                                                          progressBar.Step = 1;
                                                                      }));
                                         const int bufferSize = 32*1024; //buffer
                                         foreach (string filelist in filePaths)
                                         {
                                             var file = new FileInfo(filelist);
                                             string fileDirectory = file.DirectoryName;
                                             string filename = file.Name;
                                             var stringSeparators = new[] {sourcepath};
                                             string[] result = fileDirectory.Split(
                                                 stringSeparators, StringSplitOptions.None);
                                             int filedirectorycount = result.Length;
                                             string directoryname = "";
                                             if (filedirectorycount == 2)
                                             {
                                                 directoryname = result[1] + "\\";
                                             }
                                             CopyFile(filelist,
                                                      destinationpath + directoryname +
                                                      filename,
                                                      directoryname, bufferSize);
                                             Invoke(
                                                 new MethodInvoker(
                                                     () => progressBar.PerformStep()));
                                         }
                                         Invoke(
                                             new MethodInvoker(
                                                 delegate
                                                     {
                                                         flashLabel.Text =
                                                             @"                  Completed";
                                                     }));
                                     }
                                     catch (Exception)
                                     {
                                         Invoke(new MethodInvoker(delegate
                                                                      {
                                                                          flashLabel.Text =
                                                                              @"The network path was not found.";
                                                                          return;
                                                                      }));
                                     }
                                 };
            var th = new Thread(ts);
            th.Start();
        }
Avatar of GRChandrashekar
GRChandrashekar
Flag of India image

ASKER

and cllose method I have written this

 private static void CloseForm(Form form)
        {
            if (form.IsDisposed) return;
            if (form.InvokeRequired)
            {
                CloseMethod method = CloseForm;
                form.Invoke(method, new object[] {form});
            }
            else
            {
                form.Close();
            }
        }

        #region Nested type: CloseMethod

        private delegate void CloseMethod(Form form);

        #endregion
ASKER CERTIFIED SOLUTION
Avatar of kris_per
kris_per

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial