:sparkles:监听电源事件,自动重启Netch服务

:art:优化部分代码
This commit is contained in:
Amazing_DM
2020-04-25 18:50:41 +08:00
parent 5c10b5c92d
commit 141db8ad63
4 changed files with 49 additions and 5 deletions

View File

@@ -1,4 +1,7 @@
using System;
using Netch.Utils;
using System;
using System.Windows;
using System.Windows.Forms;
namespace Netch.Controllers
{
@@ -52,7 +55,11 @@ namespace Netch.Controllers
}
catch (Exception e)
{
Utils.Logging.Info(e.ToString());
if (System.Windows.Forms.MessageBox.Show(i18N.Translate("Failed to set the system proxy, it may be caused by the lack of dependent programs. Do you want to jump to Netch's official website to download dependent programs?"), i18N.Translate("Information"), MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
System.Diagnostics.Process.Start("https://netch.org/#/?id=%e4%be%9d%e8%b5%96");
}
Utils.Logging.Info("设置系统代理失败"+e.ToString());
return false;
}

View File

@@ -1,3 +1,4 @@
using Microsoft.Win32;
using Netch.Controllers;
using Netch.Utils;
using System;
@@ -50,13 +51,35 @@ namespace Netch.Forms
public MainForm()
{
InitializeComponent();
// 监听电源事件
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
VersionLabel.Text = UpdateChecker.Version;
CheckForIllegalCrossThreadCalls = false;
// MenuStrip.Renderer = new Override.ToolStripProfessionalRender();
Instance = this;
}
/// <summary>
/// 监听电源事件自动重启Netch服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Suspend://操作系统即将挂起
Logging.Info("操作系统即将挂起,自动停止===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
ControlFun();
break;
case PowerModes.Resume://操作系统即将从挂起状态继续
Logging.Info("操作系统即将从挂起状态继续,自动重启===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
ControlFun();
break;
}
}
private void CheckUpdate()
{
var updater = new UpdateChecker();
@@ -779,6 +802,12 @@ namespace Netch.Forms
private void ControlButton_Click(object sender, EventArgs e)
{
ControlFun();
}
public void ControlFun()
{
//聚焦到启动按钮,防止模式选择框变成蓝色:D
ControlButton.Focus();
SaveConfigs();
if (State == Models.State.Waiting || State == Models.State.Stopped)
{
@@ -807,6 +836,8 @@ namespace Netch.Forms
updateACLWithProxyToolStripMenuItem.Enabled = false;
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false;
reinstallTapDriverToolStripMenuItem.Enabled = false;
ServerComboBox.Enabled = false;
ModeComboBox.Enabled = false;
ControlButton.Text = "...";
StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting")}";
@@ -923,6 +954,8 @@ namespace Netch.Forms
updateACLWithProxyToolStripMenuItem.Enabled = true;
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true;
reinstallTapDriverToolStripMenuItem.Enabled = true;
ServerComboBox.Enabled = true;
ModeComboBox.Enabled = true;
ControlButton.Text = Utils.i18N.Translate("Start");
StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Start failed")}";
@@ -965,6 +998,8 @@ namespace Netch.Forms
updateACLWithProxyToolStripMenuItem.Enabled = true;
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true;
reinstallTapDriverToolStripMenuItem.Enabled = true;
ServerComboBox.Enabled = true;
ModeComboBox.Enabled = true;
ControlButton.Text = Utils.i18N.Translate("Start");
StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Stopped")}";
@@ -973,8 +1008,8 @@ namespace Netch.Forms
TestServer();
});
}
}
}
private void ShowMainFormToolStripButton_Click(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)

View File

@@ -1,4 +1,5 @@
using System;
using Microsoft.Win32;
using System;
using System.Globalization;
using System.IO;
using System.Text;

View File

@@ -164,6 +164,7 @@
"STUN_ServerPort value illegal. Try again.": "STUN 端口数值非法。请重试。",
"Detection interval value illegal. Try again.": "检测间隔值非法。请重试。",
"TUN/TAP driver is not detected. Is it installed now?": "未检测到 TUN/TAP 驱动,是否现在安装?",
"Failed to set the system proxy, it may be caused by the lack of dependent programs. Do you want to jump to Netch's official website to download dependent programs?": "设置系统代理失败可能是缺少依赖导致是否跳转Netch官网下载依赖程序",
"Experimental function": "实验性功能",
"Delay test after startup": "启动后延迟测试",
"Enable": "启用",