diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index 2351436460..a74c5b99d3 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; +using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; @@ -133,7 +134,15 @@ public async Task Start(Server server, Mode mode) } Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name)); - result = await Task.Run(() => pEncryptedProxyController.Start(server, mode)); + try + { + result = await Task.Run(() => pEncryptedProxyController.Start(server, mode)); + } + catch (Exception e) + { + Logging.Error("加密代理启动失败未处理异常: " + e); + result = false; + } } if (result) @@ -162,7 +171,18 @@ public async Task Start(Server server, Mode mode) if (pModeController != null) { Global.MainForm.StatusText(i18N.Translate("Starting ", pModeController.Name)); - result = await Task.Run(() => pModeController.Start(server, mode)); + try + { + result = await Task.Run(() => pModeController.Start(server, mode)); + } + catch (Exception e) + { + if (e is DllNotFoundException || e is FileNotFoundException) + MessageBoxX.Show(e.Message + "\n\n" + i18N.Translate("Missing File or runtime components"), owner: Global.MainForm); + else + Logging.Error("模式启动失败未处理异常" + e); + result = false; + } } if (result) diff --git a/Netch/Forms/MainForm.Control.cs b/Netch/Forms/MainForm.Control.cs index 5acbfac238..de10d2d421 100644 --- a/Netch/Forms/MainForm.Control.cs +++ b/Netch/Forms/MainForm.Control.cs @@ -40,22 +40,8 @@ private async void ControlFun() var server = ServerComboBox.SelectedItem as Models.Server; var mode = ModeComboBox.SelectedItem as Models.Mode; - var result = false; - try - { - // TODO 完善控制器异常处理 - result = await _mainController.Start(server, mode); - } - catch (Exception e) - { - if (e is DllNotFoundException || e is FileNotFoundException) - MessageBoxX.Show(e.Message + "\n\n" + i18N.Translate("Missing File or runtime components"), owner: this); - Netch.Application_OnException(null, new ThreadExceptionEventArgs(e)); - } - - - if (result) + if (await _mainController.Start(server, mode)) { State = State.Started; _ = Task.Run(() => { Bandwidth.NetTraffic(server, mode, ref _mainController); }); diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs index c3d8ef1380..0664c53ea8 100644 --- a/Netch/Forms/Mode/Process.cs +++ b/Netch/Forms/Mode/Process.cs @@ -11,15 +11,16 @@ public partial class Process : Form { //用于判断当前窗口是否为编辑模式 private bool EditMode; + //被编辑模式坐标 private Models.Mode EditMode_Old; + /// - /// 编辑模式 - /// - /// 模式 + /// 编辑模式 + /// + /// 模式 public Process(Models.Mode mode) { - InitializeComponent(); CheckForIllegalCrossThreadCalls = false; @@ -38,8 +39,8 @@ public Process(Models.Mode mode) FilenameTextBox.Text = mode.FileName; RemarkTextBox.Text = mode.Remark; - } + public Process() { InitializeComponent(); @@ -51,10 +52,10 @@ public Process() } /// - /// 扫描目录 - /// - /// 路径 - public void ScanDirectory(string DirName) + /// 扫描目录 + /// + /// 路径 + public void ScanDirectory(string DirName) { try { @@ -75,17 +76,25 @@ public void ScanDirectory(string DirName) while (DirStack.Count > 0) { var DirInfo = new DirectoryInfo(DirStack.Pop()); - foreach (var DirChildInfo in DirInfo.GetDirectories()) - { - DirStack.Push(DirChildInfo.FullName); - } - foreach (var FileChildInfo in DirInfo.GetFiles()) + try { - if (FileChildInfo.Name.EndsWith(".exe") && !RuleListBox.Items.Contains(FileChildInfo.Name)) + foreach (var DirChildInfo in DirInfo.GetDirectories()) { - RuleListBox.Items.Add(FileChildInfo.Name); + DirStack.Push(DirChildInfo.FullName); + } + + foreach (var FileChildInfo in DirInfo.GetFiles()) + { + if (FileChildInfo.Name.EndsWith(".exe") && !RuleListBox.Items.Contains(FileChildInfo.Name)) + { + RuleListBox.Items.Add(FileChildInfo.Name); + } } } + catch (Exception) + { + // ignored + } } } @@ -136,10 +145,11 @@ private void RuleListBox_MouseUp(object sender, MouseEventArgs e) strip.Items.Add(i18N.Translate("Delete")); if (e.Button == MouseButtons.Right) { - strip.Show(RuleListBox, e.Location);//鼠标右键按下弹出菜单 + strip.Show(RuleListBox, e.Location); //鼠标右键按下弹出菜单 strip.MouseClick += deleteRule_Click; } } + void deleteRule_Click(object sender, EventArgs e) { if (RuleListBox.SelectedIndex != -1) @@ -249,7 +259,7 @@ private void ControlButton_Click(object sender, EventArgs e) else { Global.Settings.ModeFileNameType = 2; - FilenameTextBox.Text = ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(); + FilenameTextBox.Text = ((long) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(); } Configuration.Save(); @@ -261,6 +271,7 @@ private void ControlButton_Click(object sender, EventArgs e) MessageBoxX.Show(i18N.Translate("Please enter a mode filename")); return; } + var ModeFilename = Path.Combine("mode", FilenameTextBox.Text); // 如果文件已存在,返回 @@ -332,4 +343,4 @@ private void UseCustomFileNameBox_CheckedChanged(object sender, EventArgs e) } } } -} +} \ No newline at end of file