Weird Invoke problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • msjonathan
    New Member
    • Dec 2009
    • 24

    #1

    Weird Invoke problem

    Hej hej,

    I had something very weird when I had to close our application. It came in our method with this code.
    (the invoke code was hit, but did not close our application).
    Code:
    if (InvokeRequired)
                this.Invoke((MethodInvoker)delegate { Application.Exit(); });
             else
                Application.Exit();
    When I changed the code to:
    Code:
    MethodInvoker toExecute = delegate
             {
                Application.Exit();
             };
    
             if (InvokeRequired)
                this.Invoke(toExecute);
             else
                Application.Exit();
    It worked, Does anybody know what's the difference between those pieces of code? It looks almost the same.

    Greetz,

    Jonatahn
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Originally posted by msjonathan
    Hej hej,

    I had something very weird when I had to close our application. It came in our method with this code.
    (the invoke code was hit, but did not close our application).
    Code:
    if (InvokeRequired)
                this.Invoke((MethodInvoker)delegate { Application.Exit(); });
             else
                Application.Exit();
    When I changed the code to:
    Code:
    MethodInvoker toExecute = delegate
             {
                Application.Exit();
             };
    
             if (InvokeRequired)
                this.Invoke(toExecute);
             else
                Application.Exit();
    It worked, Does anybody know what's the difference between those pieces of code? It looks almost the same.

    Greetz,

    Jonatahn
    I've tried this and both of your codes did work. Maybe there's another problem which lets your code fail.
    Did you tried the non-working code more than once?

    What's happening? Are there any error-messages or is does your program just don't do anything?

    Comment

    • msjonathan
      New Member
      • Dec 2009
      • 24

      #3
      Originally posted by ChBinder
      I've tried this and both of your codes did work. Maybe there's another problem which lets your code fail.
      Did you tried the non-working code more than once?

      What's happening? Are there any error-messages or is does your program just don't do anything?
      yes I did it like 10 times, there is not error and I am running with my debug exceptions on so I should notice if there is any error.

      When we do the first piece code the program just continues and does not exit. But when we do the second piece of code the program exits perfectly. It's a very weird problem because I think those 2 pieces of code should do exactly the same.

      Greetz

      Jonathan

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        Yes, I think so too.
        Which framework do you use? I've tried it within 3.5 but I can hardly think that this would be the cause of your error.

        You could try other things han Application.Exi t(), e.g. show a MessageBox. This is really a very weird thing ...

        Comment

        • msjonathan
          New Member
          • Dec 2009
          • 24

          #5
          I am running 3.5 also, will try with a messagebox.show ()

          maybe I have to post a little bit of code:

          Code:
          try{
          if (DateTime.Now.Date > LicenseSettings.MaxDate || DateTime.Now.Date > GalantisSettings.LockDate)
                         throw new ArgumentException("Expired!");
          
          ...
          }catch (ArgumentException)
                   {
                      #region Galantis is expired
                      ShowMessageBox(Translation.Translate(TranslationKeys.GalantisExpired, TranslationFormat.Standard), MessageBoxIcon.Stop);
                      ExitApplication();
                      #endregion
                   }
          Finally
          {
           TimeSpan tsLicense = new TimeSpan(GalantisSettings.LOCKOUT_DAYS + 1, 0, 0, 0);
                      TimeSpan tsGalantis = new TimeSpan(GalantisSettings.LOCKOUT_DAYS + 1, 0, 0, 0);
          
                      if (LicenseSettings != null)
                         tsLicense = LicenseSettings.MaxDate.Subtract(DateTime.Now.Date);
          
                      if (GalantisSettings != null)
                         tsGalantis = GalantisSettings.LockDate.Subtract(DateTime.Now.Date);
          
                      if (tsLicense.TotalDays > 0 && tsGalantis.TotalDays > 0)
                         if (tsLicense.TotalDays <= GalantisSettings.LOCKOUT_DAYS || tsGalantis.TotalDays <= GalantisSettings.LOCKOUT_DAYS)
                            ShowMessageBox(Translation.Translate(TranslationKeys.GalantisLockedDown, Math.Min(tsLicense.TotalDays, tsGalantis.TotalDays).ToString(), TranslationFormat.Standard), MessageBoxIcon.Warning);
          }
          
          private void ExitApplication()
                {
          
                   MethodInvoker toExecute = delegate
                   {
                      Application.Exit();
                   };
          
                   if (InvokeRequired)
                      this.Invoke(toExecute);
                   else
                      Application.Exit();
                }
          When we break it comes in the exitApplication () method, then we go further it goes into the finally. When we use this lines of code the program quits correct, when we use the old code the program does not quit.

          Greetz

          Jonathan
          Last edited by msjonathan; Jun 9 '10, 07:20 AM. Reason: adding some comment

          Comment

          • msjonathan
            New Member
            • Dec 2009
            • 24

            #6
            I tested it with messagebox.show and it worked, but also Application.Exi t() works also, the only thing I changed also was putting in a finally
            Code:
            # if (tsLicense.TotalDays > 0 && tsGalantis.TotalDays > 0)
            #                if (tsLicense.TotalDays <= GalantisSettings.LOCKOUT_DAYS || tsGalantis.TotalDays <= GalantisSettings.LOCKOUT_DAYS)
            #                   ShowMessageBox(Translation.Translate(TranslationKeys.GalantisLockedDown, Math.Min(tsLicense.TotalDays, tsGalantis.TotalDays).ToString(), TranslationFormat.Standard), MessageBoxIcon.Warning);
            So the messagebox was not called any more, could that be it? when a messagebox is called after a Application.exi t() the program does not stop, but keeps running?

            Edit: but when I comment my finally so no messagebox is called, the application does not exit either, Can this be a Threading problem?
            Greetz,

            Jonathan
            Last edited by msjonathan; Jun 9 '10, 07:34 AM. Reason: adding extra information

            Comment

            • Christian Binder
              Recognized Expert New Member
              • Jan 2008
              • 218

              #7
              Yes, maybe there are other Threads running. Application.Exi t waits until all Threads are finished. If another Thread is running, the GUI might bei closed, but the program isn't finished completely since all threads are done.

              Comment

              • msjonathan
                New Member
                • Dec 2009
                • 24

                #8
                ok, thanks for your help will look into it some further.

                Comment

                Working...