From e918c863b94583d25496af05cb8fac2142214715 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Wed, 14 May 2014 16:35:06 -0400 Subject: [PATCH] Add retry if Skype initialization fails --- LightController.cs | 37 +++++++++++++++++++++++++-------- Properties/Settings.Designer.cs | 11 +++++++++- Properties/Settings.settings | 3 +++ TrayIcon.cs | 10 ++++----- app.config | 10 +++++++++ 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/LightController.cs b/LightController.cs index 0accf77..633f205 100644 --- a/LightController.cs +++ b/LightController.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text.RegularExpressions; - +using System.Timers; using Common.Native; using SKYPE4COMLib; using WorkIndicator.Delcom; @@ -23,6 +23,7 @@ namespace WorkIndicator private static StoplightIndicator _stoplightIndicator; private static bool _initialized; private static Status _status = Status.Auto; + private static Timer _retryTimer; public static void Initialize() { @@ -61,9 +62,9 @@ namespace WorkIndicator private static void UpdateLights() { - StoplightIndicator.LightState red = StoplightIndicator.LightState.Off; - StoplightIndicator.LightState yellow = StoplightIndicator.LightState.Off; - StoplightIndicator.LightState green = StoplightIndicator.LightState.Off; + var red = StoplightIndicator.LightState.Off; + var yellow = StoplightIndicator.LightState.Off; + var green = StoplightIndicator.LightState.Off; if (_status == Status.Auto) { @@ -112,13 +113,31 @@ namespace WorkIndicator public static void InitializeSkypeDetection() { - _skype = new Skype(); - _skype.Attach(); + try + { + _skype = new Skype(); + _skype.Attach(); - _ISkypeEvents_Event skypeEvents = _skype; + _ISkypeEvents_Event skypeEvents = _skype; - skypeEvents.CallStatus += HandleSkypeCallStatus; - skypeEvents.Mute += HandleSkypeEventsMute; + skypeEvents.CallStatus += HandleSkypeCallStatus; + skypeEvents.Mute += HandleSkypeEventsMute; + } + catch (Exception) + { + if (_retryTimer == null) + { + _retryTimer = new Timer(Properties.Settings.Default.RetryInterval.TotalMilliseconds) { AutoReset = false }; + _retryTimer.Elapsed += HandRetryTimerElapsed; + } + + _retryTimer.Start(); + } + } + + static void HandRetryTimerElapsed(object sender, ElapsedEventArgs e) + { + InitializeSkypeDetection(); } static void HandleSkypeEventsMute(bool mute) diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs index 9d6a133..a4f363f 100644 --- a/Properties/Settings.Designer.cs +++ b/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34011 +// Runtime Version:4.0.30319.34209 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -46,5 +46,14 @@ namespace WorkIndicator.Properties { this["WindowPattern"] = value; } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("00:00:10")] + public global::System.TimeSpan RetryInterval { + get { + return ((global::System.TimeSpan)(this["RetryInterval"])); + } + } } } diff --git a/Properties/Settings.settings b/Properties/Settings.settings index 89eae76..930dcb3 100644 --- a/Properties/Settings.settings +++ b/Properties/Settings.settings @@ -8,5 +8,8 @@ .* - CKACZOR - Remote Desktop Connection + + 00:00:10 + \ No newline at end of file diff --git a/TrayIcon.cs b/TrayIcon.cs index 51cf07a..5230f80 100644 --- a/TrayIcon.cs +++ b/TrayIcon.cs @@ -17,11 +17,11 @@ namespace WorkIndicator _trayIcon = new NotifyIcon { Icon = Resources.MainIcon, Text = Resources.ApplicationName }; // Setup the menu - ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); + var contextMenuStrip = new ContextMenuStrip(); contextMenuStrip.Opening += HandleContextMenuStripOpening; // Add the menu items - ToolStripMenuItem menuItem = new ToolStripMenuItem(Resources.Auto, null, HandleStatusMenuClick) { Tag = Status.Auto }; + var menuItem = new ToolStripMenuItem(Resources.Auto, null, HandleStatusMenuClick) { Tag = Status.Auto }; contextMenuStrip.Items.Add(menuItem); // -- @@ -65,7 +65,7 @@ namespace WorkIndicator if (menuItem.Tag == null) continue; - Status status = (Status) menuItem.Tag; + var status = (Status) menuItem.Tag; ((ToolStripMenuItem) menuItem).Checked = (LightController.Status == status); } @@ -73,9 +73,9 @@ namespace WorkIndicator private static void HandleStatusMenuClick(object sender, System.EventArgs e) { - ToolStripMenuItem menuItem = (ToolStripMenuItem) sender; + var menuItem = (ToolStripMenuItem) sender; - Status status = (Status) menuItem.Tag; + var status = (Status) menuItem.Tag; LightController.Status = status; } diff --git a/app.config b/app.config index ff571e5..7b68988 100644 --- a/app.config +++ b/app.config @@ -4,6 +4,9 @@
+ +
+ @@ -15,4 +18,11 @@ + + + + 00:00:10 + + + \ No newline at end of file