Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Closing Thread

Posted on 2010-11-30
2
Medium Priority
?
489 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 2000 total points
ID: 34239949

Try adding 'return' after Application.Exit.

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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