Skip to content

Commit

Permalink
尝试修复退出错误
Browse files Browse the repository at this point in the history
修复停止时进程堵塞
  • Loading branch information
chsbuffer committed Jul 24, 2020
1 parent 6174e78 commit bbf9c5e
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 151 deletions.
7 changes: 6 additions & 1 deletion Netch/Controllers/Interface/Controller.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -67,10 +68,14 @@ protected void StopInstance()
Instance.Kill();
Instance.WaitForExit();
}
catch (Exception e)
catch (Win32Exception e)
{
Logging.Error($"停止 {MainFile} 错误:\n" + e);
}
catch
{
// ignored
}
}


Expand Down
11 changes: 8 additions & 3 deletions Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,14 @@ public bool Start(Server server, Mode mode)
/// </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)
Expand Down
31 changes: 16 additions & 15 deletions Netch/Controllers/Mode/HTTPController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using Netch.Models;
Expand Down Expand Up @@ -30,7 +31,8 @@ public HTTPController()
/// <returns>是否启动成功</returns>
public override bool Start(Server server, Mode mode)
{
RecordPrevious();
Task.Run(RecordPrevious);

try
{
if (server.Type == "Socks5")
Expand Down Expand Up @@ -79,21 +81,20 @@ private void RecordPrevious()
/// </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);
}
}
}
18 changes: 12 additions & 6 deletions Netch/Controllers/Mode/TUNTAPController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Netch.Models;
using Netch.Properties;
Expand Down Expand Up @@ -366,10 +367,11 @@ public override bool Start(Server server, Mode mode)
{
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;
}
Expand All @@ -383,9 +385,13 @@ public override bool Start(Server server, Mode mode)
/// </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>
Expand Down
8 changes: 3 additions & 5 deletions Netch/Controllers/NTTController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public NTTController()

return (true, natType, localEnd, publicEnd);
}
catch (Exception)
catch (Exception e)
{
Logging.Error("NTT 进程出错");
Logging.Error("NTT 进程出错\n" + e);
Stop();
return (false, null, null, null);
}
Expand All @@ -57,11 +57,9 @@ private new void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
_lastResult = e.Data;
}

/// <summary>
/// 无用
/// </summary>
public override void Stop()
{
StopInstance();
}
}
}
27 changes: 15 additions & 12 deletions Netch/Forms/MainForm.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ partial class MainForm

private void ControlFun()
{
//防止模式选择框变成蓝色:D
ModeComboBox.Select(0, 0);

if (State == State.Waiting || State == State.Stopped)
{
// 服务器、模式 需选择
Expand All @@ -37,7 +34,10 @@ private void ControlFun()
return;
}

UpdateStatus(State.Starting);
State = State.Starting;

// 清除模式搜索框文本选择
ModeComboBox.Select(0, 0);

Task.Run(() =>
{
Expand All @@ -50,15 +50,14 @@ private void ControlFun()
{
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)
{
Expand Down Expand Up @@ -100,16 +99,20 @@ private void ControlFun()
}
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);
}
}
Expand Down
10 changes: 5 additions & 5 deletions Netch/Forms/MainForm.MenuStrip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sende
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; })); });

Expand Down Expand Up @@ -283,7 +283,7 @@ private void CleanDNSCacheToolStripMenuItem_Click(object sender, EventArgs e)
}
finally
{
UpdateStatus(bak_State);
State = bak_State;
StatusLabel.Text = bak_StateText;
}
}
Expand Down Expand Up @@ -315,7 +315,7 @@ private void reinstallTapDriverToolStripMenuItem_Click(object sender, EventArgs
}
finally
{
UpdateStatus(State.Waiting);
State = State.Waiting;
Enabled = true;
}
});
Expand Down Expand Up @@ -365,7 +365,7 @@ private void updateACLWithProxyToolStripMenuItem_Click(object sender, EventArgs
}
finally
{
UpdateStatus(State.Waiting);
State = State.Waiting;
_mainController.Stop();
}
});
Expand Down Expand Up @@ -422,7 +422,7 @@ private void updateACLToolStripMenuItem_Click(object sender, EventArgs e)
}
finally
{
UpdateStatus(bak_State);
State = bak_State;
StatusLabel.Text = bak_StateText;
}
});
Expand Down

0 comments on commit bbf9c5e

Please sign in to comment.