mirror of
https://github.com/netchx/netch.git
synced 2026-03-18 18:13:21 +08:00
尝试修复退出错误
修复停止时进程堵塞
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
@@ -67,10 +68,14 @@ namespace Netch.Controllers
|
||||
Instance.Kill();
|
||||
Instance.WaitForExit();
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (Win32Exception e)
|
||||
{
|
||||
Logging.Error($"停止 {MainFile} 错误:\n" + e);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -146,9 +146,14 @@ namespace Netch.Controllers
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
Task.Run(() => pEncryptedProxyController?.Stop());
|
||||
Task.Run(() => UsingPorts.Clear());
|
||||
pModeController?.Stop();
|
||||
var tasks = new[]
|
||||
{
|
||||
Task.Factory.StartNew(() => pEncryptedProxyController?.Stop()),
|
||||
Task.Factory.StartNew(() => UsingPorts.Clear()),
|
||||
Task.Factory.StartNew(() => pModeController?.Stop()),
|
||||
Task.Factory.StartNew(() => pNTTController.Stop())
|
||||
};
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
public static void KillProcessByName(string name)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.Win32;
|
||||
using Netch.Models;
|
||||
@@ -30,7 +31,8 @@ namespace Netch.Controllers
|
||||
/// <returns>是否启动成功</returns>
|
||||
public override bool Start(Server server, Mode mode)
|
||||
{
|
||||
RecordPrevious();
|
||||
Task.Run(RecordPrevious);
|
||||
|
||||
try
|
||||
{
|
||||
if (server.Type == "Socks5")
|
||||
@@ -79,21 +81,20 @@ namespace Netch.Controllers
|
||||
/// </summary>
|
||||
public override void Stop()
|
||||
{
|
||||
try
|
||||
var tasks = new[]
|
||||
{
|
||||
pPrivoxyController.Stop();
|
||||
|
||||
NativeMethods.SetGlobal(prevHTTP, prevBypass);
|
||||
if (prevPAC != "")
|
||||
NativeMethods.SetURL(prevPAC);
|
||||
if (!prevEnabled)
|
||||
NativeMethods.SetDIRECT();
|
||||
prevEnabled = false;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error("停止HTTP控制器错误:\n" + e);
|
||||
}
|
||||
Task.Factory.StartNew(pPrivoxyController.Stop),
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
NativeMethods.SetGlobal(prevHTTP, prevBypass);
|
||||
if (prevPAC != "")
|
||||
NativeMethods.SetURL(prevPAC);
|
||||
if (!prevEnabled)
|
||||
NativeMethods.SetDIRECT();
|
||||
prevEnabled = false;
|
||||
})
|
||||
};
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Netch.Models;
|
||||
using Netch.Properties;
|
||||
@@ -366,10 +367,11 @@ namespace Netch.Controllers
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
|
||||
if (State == State.Started) return true;
|
||||
|
||||
if (State == State.Stopped)
|
||||
switch (State)
|
||||
{
|
||||
case State.Started:
|
||||
return true;
|
||||
case State.Stopped:
|
||||
Stop();
|
||||
return false;
|
||||
}
|
||||
@@ -383,9 +385,13 @@ namespace Netch.Controllers
|
||||
/// </summary>
|
||||
public override void Stop()
|
||||
{
|
||||
StopInstance();
|
||||
ClearBypass();
|
||||
pDNSController.Stop();
|
||||
var tasks = new[]
|
||||
{
|
||||
Task.Factory.StartNew(StopInstance),
|
||||
Task.Factory.StartNew(ClearBypass),
|
||||
Task.Factory.StartNew(pDNSController.Stop)
|
||||
};
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -43,9 +43,9 @@ namespace Netch.Controllers
|
||||
|
||||
return (true, natType, localEnd, publicEnd);
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error("NTT 进程出错");
|
||||
Logging.Error("NTT 进程出错\n" + e);
|
||||
Stop();
|
||||
return (false, null, null, null);
|
||||
}
|
||||
@@ -57,11 +57,9 @@ namespace Netch.Controllers
|
||||
_lastResult = e.Data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 无用
|
||||
/// </summary>
|
||||
public override void Stop()
|
||||
{
|
||||
StopInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,6 @@ namespace Netch.Forms
|
||||
|
||||
private void ControlFun()
|
||||
{
|
||||
//防止模式选择框变成蓝色:D
|
||||
ModeComboBox.Select(0, 0);
|
||||
|
||||
if (State == State.Waiting || State == State.Stopped)
|
||||
{
|
||||
// 服务器、模式 需选择
|
||||
@@ -37,7 +34,10 @@ namespace Netch.Forms
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateStatus(State.Starting);
|
||||
State = State.Starting;
|
||||
|
||||
// 清除模式搜索框文本选择
|
||||
ModeComboBox.Select(0, 0);
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
@@ -50,15 +50,14 @@ namespace Netch.Forms
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
UpdateStatus(State.Started,
|
||||
i18N.Translate(StateExtension.GetStatusString(State.Started)) + LocalPortText(server.Type, mode.Type));
|
||||
State = State.Started;
|
||||
StatusTextAppend(LocalPortText(server.Type, mode.Type));
|
||||
Bandwidth.NetTraffic(server, mode, _mainController);
|
||||
});
|
||||
// 如果勾选启动后最小化
|
||||
if (Global.Settings.MinimizeWhenStarted)
|
||||
{
|
||||
WindowState = FormWindowState.Minimized;
|
||||
NotifyIcon.Visible = true;
|
||||
|
||||
if (_isFirstCloseWindow)
|
||||
{
|
||||
@@ -100,16 +99,20 @@ namespace Netch.Forms
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateStatus(State.Stopped, i18N.Translate("Start failed"));
|
||||
State = State.Stopped;
|
||||
StatusText(i18N.Translate("Start failed"));
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// 停止
|
||||
UpdateStatus(State.Stopping);
|
||||
_mainController.Stop();
|
||||
UpdateStatus(State.Stopped);
|
||||
Task.Run(() =>
|
||||
{
|
||||
// 停止
|
||||
State = State.Stopping;
|
||||
_mainController.Stop();
|
||||
State = State.Stopped;
|
||||
});
|
||||
Task.Run(TestServer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +210,7 @@ namespace Netch.Forms
|
||||
StatusText(i18N.Translate("Subscription updated"));
|
||||
|
||||
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
|
||||
UpdateStatus(bak_State);
|
||||
State = bak_State;
|
||||
StatusLabel.Text = bak_StateText;
|
||||
}).ContinueWith(task => { BeginInvoke(new Action(() => { UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; })); });
|
||||
|
||||
@@ -283,7 +283,7 @@ namespace Netch.Forms
|
||||
}
|
||||
finally
|
||||
{
|
||||
UpdateStatus(bak_State);
|
||||
State = bak_State;
|
||||
StatusLabel.Text = bak_StateText;
|
||||
}
|
||||
}
|
||||
@@ -315,7 +315,7 @@ namespace Netch.Forms
|
||||
}
|
||||
finally
|
||||
{
|
||||
UpdateStatus(State.Waiting);
|
||||
State = State.Waiting;
|
||||
Enabled = true;
|
||||
}
|
||||
});
|
||||
@@ -365,7 +365,7 @@ namespace Netch.Forms
|
||||
}
|
||||
finally
|
||||
{
|
||||
UpdateStatus(State.Waiting);
|
||||
State = State.Waiting;
|
||||
_mainController.Stop();
|
||||
}
|
||||
});
|
||||
@@ -422,7 +422,7 @@ namespace Netch.Forms
|
||||
}
|
||||
finally
|
||||
{
|
||||
UpdateStatus(bak_State);
|
||||
State = bak_State;
|
||||
StatusLabel.Text = bak_StateText;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Threading;
|
||||
using Netch.Models;
|
||||
using Netch.Utils;
|
||||
|
||||
@@ -11,10 +12,74 @@ namespace Netch.Forms
|
||||
|
||||
partial class MainForm
|
||||
{
|
||||
private State _state = State.Waiting;
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态
|
||||
/// </summary>
|
||||
public State State { get; private set; } = State.Waiting;
|
||||
public State State
|
||||
{
|
||||
get => _state;
|
||||
private set
|
||||
{
|
||||
_state = value;
|
||||
if (IsDisposed)
|
||||
return;
|
||||
StatusText(i18N.Translate(StateExtension.GetStatusString(value)));
|
||||
switch (value)
|
||||
{
|
||||
case State.Waiting:
|
||||
ControlButton.Enabled = true;
|
||||
ControlButton.Text = i18N.Translate("Start");
|
||||
|
||||
break;
|
||||
case State.Starting:
|
||||
ControlButton.Enabled = false;
|
||||
ControlButton.Text = "...";
|
||||
|
||||
ConfigurationGroupBox.Enabled = false;
|
||||
|
||||
MenuStripsEnabled(false);
|
||||
break;
|
||||
case State.Started:
|
||||
ControlButton.Enabled = true;
|
||||
ControlButton.Text = i18N.Translate("Stop");
|
||||
|
||||
LastUploadBandwidth = 0;
|
||||
//LastDownloadBandwidth = 0;
|
||||
//UploadSpeedLabel.Text = "↑: 0 KB/s";
|
||||
DownloadSpeedLabel.Text = @"↑↓: 0 KB/s";
|
||||
UsedBandwidthLabel.Text = $@"{i18N.Translate("Used", ": ")}0 KB";
|
||||
UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = true;
|
||||
break;
|
||||
case State.Stopping:
|
||||
ControlButton.Enabled = false;
|
||||
ControlButton.Text = "...";
|
||||
|
||||
ProfileGroupBox.Enabled = false;
|
||||
|
||||
UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = false;
|
||||
NatTypeStatusText();
|
||||
break;
|
||||
case State.Stopped:
|
||||
ControlButton.Enabled = true;
|
||||
ControlButton.Text = i18N.Translate("Start");
|
||||
|
||||
LastUploadBandwidth = 0;
|
||||
LastDownloadBandwidth = 0;
|
||||
|
||||
ProfileGroupBox.Enabled = true;
|
||||
ConfigurationGroupBox.Enabled = true;
|
||||
|
||||
MenuStripsEnabled(true);
|
||||
break;
|
||||
case State.Terminating:
|
||||
Dispose();
|
||||
Environment.Exit(Environment.ExitCode);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void NatTypeStatusText(string text = "", string country = "")
|
||||
{
|
||||
@@ -89,85 +154,18 @@ namespace Netch.Forms
|
||||
StatusLabel.Text = i18N.Translate("Status", ": ") + text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 UI, 状态栏文本, 状态
|
||||
/// </summary>
|
||||
/// <param name="state"></param>
|
||||
/// <param name="text"></param>
|
||||
private void UpdateStatus(State state, string text = "")
|
||||
public void StatusTextAppend(string text)
|
||||
{
|
||||
State = state;
|
||||
StatusText(text == "" ? i18N.Translate(StateExtension.GetStatusString(state)) : text);
|
||||
|
||||
void MenuStripsEnabled(bool enabled)
|
||||
{
|
||||
// 需要禁用的菜单项
|
||||
UninstallServiceToolStripMenuItem.Enabled =
|
||||
updateACLWithProxyToolStripMenuItem.Enabled =
|
||||
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled =
|
||||
reinstallTapDriverToolStripMenuItem.Enabled = enabled;
|
||||
}
|
||||
|
||||
// TODO 补充
|
||||
switch (state)
|
||||
{
|
||||
case State.Waiting:
|
||||
ControlButton.Enabled = true;
|
||||
ControlButton.Text = i18N.Translate("Start");
|
||||
|
||||
break;
|
||||
case State.Starting:
|
||||
ControlButton.Enabled = false;
|
||||
ControlButton.Text = "...";
|
||||
|
||||
ConfigurationGroupBox.Enabled = false;
|
||||
|
||||
MenuStripsEnabled(false);
|
||||
break;
|
||||
case State.Started:
|
||||
ControlButton.Enabled = true;
|
||||
ControlButton.Text = i18N.Translate("Stop");
|
||||
|
||||
LastUploadBandwidth = 0;
|
||||
//LastDownloadBandwidth = 0;
|
||||
//UploadSpeedLabel.Text = "↑: 0 KB/s";
|
||||
DownloadSpeedLabel.Text = @"↑↓: 0 KB/s";
|
||||
UsedBandwidthLabel.Text = $@"{i18N.Translate("Used", ": ")}0 KB";
|
||||
UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = true;
|
||||
break;
|
||||
case State.Stopping:
|
||||
ControlButton.Enabled = false;
|
||||
ControlButton.Text = "...";
|
||||
|
||||
ProfileGroupBox.Enabled = false;
|
||||
|
||||
UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = false;
|
||||
NatTypeStatusText();
|
||||
break;
|
||||
case State.Stopped:
|
||||
ControlButton.Enabled = true;
|
||||
ControlButton.Text = i18N.Translate("Start");
|
||||
|
||||
LastUploadBandwidth = 0;
|
||||
LastDownloadBandwidth = 0;
|
||||
|
||||
ProfileGroupBox.Enabled = true;
|
||||
ConfigurationGroupBox.Enabled = true;
|
||||
|
||||
MenuStripsEnabled(true);
|
||||
break;
|
||||
case State.Terminating:
|
||||
|
||||
break;
|
||||
}
|
||||
StatusLabel.Text += text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新 UI
|
||||
/// </summary>
|
||||
private void UpdateStatus()
|
||||
public void MenuStripsEnabled(bool enabled)
|
||||
{
|
||||
UpdateStatus(State);
|
||||
// 需要禁用的菜单项
|
||||
UninstallServiceToolStripMenuItem.Enabled =
|
||||
updateACLWithProxyToolStripMenuItem.Enabled =
|
||||
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled =
|
||||
reinstallTapDriverToolStripMenuItem.Enabled = enabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,6 @@ namespace Netch.Forms
|
||||
{
|
||||
// 使关闭时窗口向右下角缩小的效果
|
||||
WindowState = FormWindowState.Minimized;
|
||||
NotifyIcon.Visible = true;
|
||||
|
||||
if (_isFirstCloseWindow)
|
||||
{
|
||||
@@ -211,39 +210,31 @@ namespace Netch.Forms
|
||||
// 加载翻译
|
||||
|
||||
UsedBandwidthLabel.Text = $@"{i18N.Translate("Used", ": ")}0 KB";
|
||||
UpdateStatus();
|
||||
State = State;
|
||||
|
||||
VersionLabel.Text = UpdateChecker.Version;
|
||||
}
|
||||
|
||||
private void Exit(bool forceExit = false)
|
||||
{
|
||||
if (IsDisposed) return;
|
||||
|
||||
// 已启动
|
||||
if (State != State.Waiting && State != State.Stopped)
|
||||
if (State != State.Waiting && State != State.Stopped && !Global.Settings.StopWhenExited && !forceExit)
|
||||
{
|
||||
if (Global.Settings.StopWhenExited || forceExit)
|
||||
{
|
||||
UpdateStatus(State.Stopping);
|
||||
ControlFun();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 未开启自动停止
|
||||
MessageBoxX.Show(i18N.Translate("Please press Stop button first"));
|
||||
MessageBoxX.Show(i18N.Translate("Please press Stop button first"));
|
||||
|
||||
Visible = true;
|
||||
ShowInTaskbar = true; // 显示在系统任务栏
|
||||
WindowState = FormWindowState.Normal; // 还原窗体
|
||||
NotifyIcon.Visible = true; // 托盘图标隐藏
|
||||
|
||||
return;
|
||||
}
|
||||
Visible = true;
|
||||
ShowInTaskbar = true; // 显示在系统任务栏
|
||||
WindowState = FormWindowState.Normal; // 还原窗体
|
||||
NotifyIcon.Visible = true; // 托盘图标隐藏
|
||||
return;
|
||||
}
|
||||
|
||||
NotifyIcon.Visible = false;
|
||||
Hide();
|
||||
NotifyIcon.Visible = false;
|
||||
if (State != State.Waiting && State != State.Stopped)
|
||||
{
|
||||
// 已启动
|
||||
ControlFun();
|
||||
}
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
@@ -255,9 +246,7 @@ namespace Netch.Forms
|
||||
}
|
||||
|
||||
SaveConfigs();
|
||||
UpdateStatus(State.Terminating);
|
||||
Dispose();
|
||||
Environment.Exit(Environment.ExitCode);
|
||||
State = State.Terminating;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -439,7 +428,6 @@ namespace Netch.Forms
|
||||
Visible = true;
|
||||
ShowInTaskbar = true; // 显示在系统任务栏
|
||||
WindowState = FormWindowState.Normal; // 还原窗体
|
||||
NotifyIcon.Visible = true; // 托盘图标隐藏
|
||||
}
|
||||
|
||||
Activate();
|
||||
|
||||
@@ -376,6 +376,11 @@ namespace Netch.Forms
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
if (port == originPort)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (PortHelper.PortInUse(port, portType))
|
||||
{
|
||||
MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", portName));
|
||||
|
||||
@@ -75,7 +75,7 @@
|
||||
|
||||
"Options": "选项",
|
||||
"Uninstall NF Service": "卸载 NF 服务",
|
||||
"Uninstalling NF Service": "正在 NF 卸载服务中",
|
||||
"Uninstalling NF Service": "正在卸载 NF 服务中",
|
||||
"Service has been uninstalled": "服务已卸载",
|
||||
"Reload Modes": "重载模式",
|
||||
"Modes have been reload": "模式已重载",
|
||||
|
||||
Reference in New Issue
Block a user