Solved

Closing Thread

Posted on 2010-11-30
2
486 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
C# Linq - Join two objects into one 3 50
Set custom font WPF 2 24
Build a string of emails from a gridview 2 27
Web page design problem 3 37
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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