By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,880 Members | 676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,880 IT Pros & Developers. It's quick & easy.

Weird Invoke problem

msjonathan
P: 24
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).
Expand|Select|Wrap|Line Numbers
  1. if (InvokeRequired)
  2.             this.Invoke((MethodInvoker)delegate { Application.Exit(); });
  3.          else
  4.             Application.Exit();
  5.  
When I changed the code to:
Expand|Select|Wrap|Line Numbers
  1. MethodInvoker toExecute = delegate
  2.          {
  3.             Application.Exit();
  4.          };
  5.  
  6.          if (InvokeRequired)
  7.             this.Invoke(toExecute);
  8.          else
  9.             Application.Exit();
  10.  
It worked, Does anybody know what's the difference between those pieces of code? It looks almost the same.

Greetz,

Jonatahn
Jun 8 '10 #1
Share this Question
Share on Google+
7 Replies

Expert 100+
P: 218
@msjonathan
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?
Jun 9 '10 #2

msjonathan
P: 24
@ChBinder
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
Jun 9 '10 #3

Expert 100+
P: 218
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.Exit(), e.g. show a MessageBox. This is really a very weird thing ...
Jun 9 '10 #4

msjonathan
P: 24
I am running 3.5 also, will try with a messagebox.show()

maybe I have to post a little bit of code:

Expand|Select|Wrap|Line Numbers
  1. try{
  2. if (DateTime.Now.Date > LicenseSettings.MaxDate || DateTime.Now.Date > GalantisSettings.LockDate)
  3.                throw new ArgumentException("Expired!");
  4.  
  5. ...
  6. }catch (ArgumentException)
  7.          {
  8.             #region Galantis is expired
  9.             ShowMessageBox(Translation.Translate(TranslationKeys.GalantisExpired, TranslationFormat.Standard), MessageBoxIcon.Stop);
  10.             ExitApplication();
  11.             #endregion
  12.          }
  13. Finally
  14. {
  15.  TimeSpan tsLicense = new TimeSpan(GalantisSettings.LOCKOUT_DAYS + 1, 0, 0, 0);
  16.             TimeSpan tsGalantis = new TimeSpan(GalantisSettings.LOCKOUT_DAYS + 1, 0, 0, 0);
  17.  
  18.             if (LicenseSettings != null)
  19.                tsLicense = LicenseSettings.MaxDate.Subtract(DateTime.Now.Date);
  20.  
  21.             if (GalantisSettings != null)
  22.                tsGalantis = GalantisSettings.LockDate.Subtract(DateTime.Now.Date);
  23.  
  24.             if (tsLicense.TotalDays > 0 && tsGalantis.TotalDays > 0)
  25.                if (tsLicense.TotalDays <= GalantisSettings.LOCKOUT_DAYS || tsGalantis.TotalDays <= GalantisSettings.LOCKOUT_DAYS)
  26.                   ShowMessageBox(Translation.Translate(TranslationKeys.GalantisLockedDown, Math.Min(tsLicense.TotalDays, tsGalantis.TotalDays).ToString(), TranslationFormat.Standard), MessageBoxIcon.Warning);
  27. }
  28.  
  29. private void ExitApplication()
  30.       {
  31.  
  32.          MethodInvoker toExecute = delegate
  33.          {
  34.             Application.Exit();
  35.          };
  36.  
  37.          if (InvokeRequired)
  38.             this.Invoke(toExecute);
  39.          else
  40.             Application.Exit();
  41.       }
  42.  
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
Jun 9 '10 #5

msjonathan
P: 24
I tested it with messagebox.show and it worked, but also Application.Exit() works also, the only thing I changed also was putting in a finally
Expand|Select|Wrap|Line Numbers
  1. # if (tsLicense.TotalDays > 0 && tsGalantis.TotalDays > 0)
  2. #                if (tsLicense.TotalDays <= GalantisSettings.LOCKOUT_DAYS || tsGalantis.TotalDays <= GalantisSettings.LOCKOUT_DAYS)
  3. #                   ShowMessageBox(Translation.Translate(TranslationKeys.GalantisLockedDown, Math.Min(tsLicense.TotalDays, tsGalantis.TotalDays).ToString(), TranslationFormat.Standard), MessageBoxIcon.Warning);
  4.  
So the messagebox was not called any more, could that be it? when a messagebox is called after a Application.exit() 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
Jun 9 '10 #6

Expert 100+
P: 218
Yes, maybe there are other Threads running. Application.Exit 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.
Jun 9 '10 #7

msjonathan
P: 24
ok, thanks for your help will look into it some further.
Jun 9 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.