Skip to content

Commit

Permalink
扫描文件夹异常处理,主控制器异常处理改进
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Aug 8, 2020
1 parent 764009e commit e5e7892
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
24 changes: 22 additions & 2 deletions Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,7 +134,15 @@ public async Task<bool> 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)
Expand Down Expand Up @@ -162,7 +171,18 @@ public async Task<bool> 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)
Expand Down
16 changes: 1 addition & 15 deletions Netch/Forms/MainForm.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
Expand Down
49 changes: 30 additions & 19 deletions Netch/Forms/Mode/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ public partial class Process : Form
{
//用于判断当前窗口是否为编辑模式
private bool EditMode;

//被编辑模式坐标
private Models.Mode EditMode_Old;

/// <summary>
/// 编辑模式
/// </summary>
/// <param name="mode">模式</param>
/// 编辑模式
/// </summary>
/// <param name="mode">模式</param>
public Process(Models.Mode mode)
{

InitializeComponent();

CheckForIllegalCrossThreadCalls = false;
Expand All @@ -38,8 +39,8 @@ public Process(Models.Mode mode)

FilenameTextBox.Text = mode.FileName;
RemarkTextBox.Text = mode.Remark;

}

public Process()
{
InitializeComponent();
Expand All @@ -51,10 +52,10 @@ public Process()
}

/// <summary>
/// 扫描目录
/// </summary>
/// <param name="DirName">路径</param>
public void ScanDirectory(string DirName)
/// 扫描目录
/// </summary>
/// <param name="DirName">路径</param>
public void ScanDirectory(string DirName)
{
try
{
Expand All @@ -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
}
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -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);

// 如果文件已存在,返回
Expand Down Expand Up @@ -332,4 +343,4 @@ private void UseCustomFileNameBox_CheckedChanged(object sender, EventArgs e)
}
}
}
}
}

1 comment on commit e5e7892

@chsbuffer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed #333

Please sign in to comment.