监听程序
Posted yyl001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听程序相关的知识,希望对你有一定的参考价值。
//运行监听程序
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 { }
}
以上是关于监听程序的主要内容,如果未能解决你的问题,请参考以下文章