Skip to content

Commit

Permalink
Simplify SystemEvents_PowerModeChanged()
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron533 committed Dec 3, 2017
1 parent 46d40fe commit c24714e
Showing 1 changed file with 13 additions and 35 deletions.
48 changes: 13 additions & 35 deletions shadowsocks-csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,19 @@ private static void SystemEvents_PowerModeChanged(object sender, PowerModeChange
Logging.Info("os wake up");
if (MainController != null)
{
System.Timers.Timer timer = new System.Timers.Timer(10 * 1000);
timer.Elapsed += Timer_Elapsed;
timer.AutoReset = false;
timer.Enabled = true;
timer.Start();
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Thread.Sleep(10 * 1000);
try
{
MainController.Start();
Logging.Info("controller started");
}
catch (Exception ex)
{
Logging.LogUsefulException(ex);
}
});
}
break;
case PowerModes.Suspend:
Expand All @@ -140,36 +148,6 @@ private static void SystemEvents_PowerModeChanged(object sender, PowerModeChange
}
}

private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
if (MainController != null)
{
MainController.Start();
Logging.Info("controller started");
}
}
catch (Exception ex)
{
Logging.LogUsefulException(ex);
}
finally
{
try
{
System.Timers.Timer timer = (System.Timers.Timer)sender;
timer.Enabled = false;
timer.Stop();
timer.Dispose();
}
catch (Exception ex)
{
Logging.LogUsefulException(ex);
}
}
}

private static void Application_ApplicationExit(object sender, EventArgs e)
{
// detach static event handlers
Expand Down

0 comments on commit c24714e

Please sign in to comment.