Solved

Closing Thread

Posted on 2010-11-30
2
487 Views
Last Modified: 2012-05-10
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();
        }
0
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:GRChandrashekar
ID: 34239221
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
0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 34239949

Try adding 'return' after Application.Exit.

...
    CloseForm(this);
   Application.Exit();
   return;
}
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question