Guest User

Untitled

a guest
Jun 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1.         private void SafeInvoker(MethodInvoker invokeMe)
  2.         {
  3.             if (this.InvokeRequired)
  4.             {
  5.                 try
  6.                 {
  7.                     int counter = 0;
  8.                     if (!this.IsDisposed)
  9.                         while (!this.IsHandleCreated)
  10.                         {
  11.                             if (exiting) return;
  12.                             Thread.Sleep(100);
  13.                             if (++counter > 150)
  14.                             {
  15.                                 log.Error("Service Form Handle was not created for 15 seconds");
  16.                                 return;
  17.                             }
  18.                         }
  19.  
  20.                     if (!this.IsDisposed && !exiting) this.Invoke(invokeMe);
  21.                 }
  22.                 catch (Exception e)
  23.                 {
  24.                     log.ErrorFormat("SafeInvoker Exception:\r\nException Message: {0}\r\nStacktrace: {1}", e.Message, e.StackTrace);
  25.                 }
  26.             }
  27.             else
  28.             {
  29.                 invokeMe();
  30.             }
  31.         }
  32.  
  33.  
  34. //example usage
  35.  
  36.                 SafeInvoker(delegate
  37.                 {
  38.                     buttonIndicator.IsOn = myProfile.ButtonAlarm != null;
  39.                     keyIndicator.IsOn = myProfile.KeyAlarm != null;
  40.                     messengerIndicator.IsOn = myProfile.Messenger != null;
  41.                 });
Add Comment
Please, Sign In to add comment