Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void SafeInvoker(MethodInvoker invokeMe)
- {
- if (this.InvokeRequired)
- {
- try
- {
- int counter = 0;
- if (!this.IsDisposed)
- while (!this.IsHandleCreated)
- {
- if (exiting) return;
- Thread.Sleep(100);
- if (++counter > 150)
- {
- log.Error("Service Form Handle was not created for 15 seconds");
- return;
- }
- }
- if (!this.IsDisposed && !exiting) this.Invoke(invokeMe);
- }
- catch (Exception e)
- {
- log.ErrorFormat("SafeInvoker Exception:\r\nException Message: {0}\r\nStacktrace: {1}", e.Message, e.StackTrace);
- }
- }
- else
- {
- invokeMe();
- }
- }
- //example usage
- SafeInvoker(delegate
- {
- buttonIndicator.IsOn = myProfile.ButtonAlarm != null;
- keyIndicator.IsOn = myProfile.KeyAlarm != null;
- messengerIndicator.IsOn = myProfile.Messenger != null;
- });
Add Comment
Please, Sign In to add comment