change controller status if needed

- Change controller based on power mode
- Stop before exiting

Based on GangZhuo's code

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
Syrone Wong
2016-09-12 16:23:03 +08:00
parent 42b2308374
commit ab464ca8b1
+30 -1
View File
@@ -3,6 +3,7 @@ using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
using Shadowsocks.Controller;
using Shadowsocks.Util;
@@ -12,6 +13,8 @@ namespace Shadowsocks
{
static class Program
{
static ShadowsocksController controller;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
@@ -30,6 +33,8 @@ namespace Shadowsocks
using (Mutex mutex = new Mutex(false, "Global\\Shadowsocks_" + Application.StartupPath.GetHashCode()))
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ApplicationExit += Application_ApplicationExit;
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
@@ -56,7 +61,7 @@ namespace Shadowsocks
#else
Logging.OpenLogFile();
#endif
ShadowsocksController controller = new ShadowsocksController();
controller = new ShadowsocksController();
MenuViewController viewController = new MenuViewController(controller);
controller.Start();
Application.Run();
@@ -76,5 +81,29 @@ namespace Shadowsocks
Application.Exit();
}
}
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Resume:
Logging.Info("os wake up");
controller?.Start();
break;
case PowerModes.Suspend:
controller?.Stop();
Logging.Info("os suspend");
break;
}
}
private static void Application_ApplicationExit(object sender, EventArgs e)
{
if (controller != null)
{
controller.Stop();
controller = null;
}
}
}
}