监听程序
//运行监听程序
ThreadPool.QueueUserWorkItem((x) =>
{
try
{
bool sqd = true;
Process[] SERVER = Process.GetProcessesByName("MonitorServer"); //获取线程名相同
foreach (Process item in SERVER)
{
if (Path.GetDirectoryName(item.MainModule.FileName).Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.Replace("/", "\\"))))
{
sqd = false;
break;
}
}
if (sqd)
{
ProcessStartInfo ps = new ProcessStartInfo("MonitorServer.exe");
ps.UseShellExecute = false;
ps.WindowStyle = ProcessWindowStyle.Hidden; //隐藏线程
Process.Start(ps);
}
}
catch { }
}, new object());
关闭窗体 同时关闭监听程序
private void ServerAuto_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (e.CloseReason == CloseReason.WindowsShutDown || e.CloseReason == CloseReason.UserClosing)
{
Application.ExitThread();
Application.Exit();
//Process.GetProcessesByName("MonitorServer")[0].Kill();
Process[] SERVER = Process.GetProcessesByName("MonitorServer");
foreach (Process item in SERVER)
{
if (Path.GetDirectoryName(item.MainModule.FileName).Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.Replace("/", "\\"))))
{
item.Kill();
}
}
Process[] CLIENT = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
foreach (Process item in CLIENT)
{
if (Path.GetDirectoryName(item.MainModule.FileName).Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.Replace("/", "\\"))))
{
item.Kill();
}
}
}
}
catch { }
}