Update V2rayHandler.cs

pull/354/head
2dust 2019-12-24 09:01:13 +08:00
parent 6a0bfc43dd
commit 97d7229974
1 changed files with 56 additions and 14 deletions

View File

@ -23,7 +23,8 @@ namespace v2rayN.Handler
private static string v2rayConfigRes = Global.v2rayConfigFileName; private static string v2rayConfigRes = Global.v2rayConfigFileName;
private List<string> lstV2ray; private List<string> lstV2ray;
public event ProcessDelegate ProcessEvent; public event ProcessDelegate ProcessEvent;
private int processId = 0; //private int processId = 0;
private Process _process;
public V2rayHandler() public V2rayHandler()
{ {
@ -90,27 +91,49 @@ namespace v2rayN.Handler
{ {
try try
{ {
bool blExist = true; if (_process != null)
if (processId > 0)
{ {
Process p1 = Process.GetProcessById(processId); KillProcess(_process);
if (p1 != null) _process.Dispose();
{ _process = null;
p1.Kill();
blExist = false;
}
} }
if (blExist) else
{ {
foreach (string vName in lstV2ray) foreach (string vName in lstV2ray)
{ {
Process[] killPro = Process.GetProcessesByName(vName); Process[] existing = Process.GetProcessesByName(vName);
foreach (Process p in killPro) foreach (Process p in existing)
{ {
p.Kill(); var path = p.MainModule.FileName;
if (path == $"{Utils.GetPath(vName)}.exe")
{
KillProcess(p);
}
} }
} }
} }
//bool blExist = true;
//if (processId > 0)
//{
// Process p1 = Process.GetProcessById(processId);
// if (p1 != null)
// {
// p1.Kill();
// blExist = false;
// }
//}
//if (blExist)
//{
// foreach (string vName in lstV2ray)
// {
// Process[] killPro = Process.GetProcessesByName(vName);
// foreach (Process p in killPro)
// {
// p.Kill();
// }
// }
//}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -163,7 +186,8 @@ namespace v2rayN.Handler
}); });
p.Start(); p.Start();
p.BeginOutputReadLine(); p.BeginOutputReadLine();
processId = p.Id; //processId = p.Id;
_process = p;
Global.processJob.AddProcess(p.Handle); Global.processJob.AddProcess(p.Handle);
} }
@ -187,5 +211,23 @@ namespace v2rayN.Handler
ProcessEvent(notify, msg); ProcessEvent(notify, msg);
} }
} }
private void KillProcess(Process p)
{
try
{
p.CloseMainWindow();
p.WaitForExit(100);
if (!p.HasExited)
{
p.Kill();
p.WaitForExit();
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
} }
} }