优化监测程序逻辑
监控程序的逻辑: 定时1min监控上位机程序有没有掉线,掉线了对程序的几个端口号进行扫描,若存在占用则进行5分钟的扫描,5分钟后还在就重启工控机;若不存在占用则重新打开上位机程序
This commit is contained in:
parent
8cd91e6fd8
commit
19da9de4ff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -5,9 +5,13 @@ using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
namespace MonitoringProgram
|
||||
{
|
||||
@ -22,10 +26,26 @@ namespace MonitoringProgram
|
||||
this.Left = -10000; this.Top = -10000;//宽高属性窗口位置放置到桌面界面外
|
||||
this.Opacity = 0;//最重要的一个属性 不透明度
|
||||
|
||||
string exePath = "F:\\Code\\yibayiyi\\ZTTMS_Manage_yibayiyi_KeepaliveTest3_DN_V1.0\\ZTTMS_Manage_yibayiyi_20230320\\bin\\Debug\\ZTTMS_Manage_yibayiyi_20230320.exe";
|
||||
|
||||
Process[] process = Process.GetProcessesByName("ZTTMS_Manage_yibayiyi_20230320");
|
||||
if (process.Length == 0)
|
||||
{
|
||||
bool isUse = PortInUse(55000);
|
||||
Console.WriteLine("端口占用情况:" + isUse.ToString());
|
||||
if (isUse)
|
||||
{
|
||||
//端口被占用
|
||||
portInUseTime++;
|
||||
}
|
||||
else
|
||||
{
|
||||
var target = Process.Start(exePath, "auto");
|
||||
targetProcessId = target.Id;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
targetProcessId = process[0].Id;
|
||||
}
|
||||
|
||||
Console.WriteLine(targetProcessId);
|
||||
|
||||
@ -37,31 +57,26 @@ namespace MonitoringProgram
|
||||
|
||||
//PID
|
||||
int targetProcessId = 0;
|
||||
string exePath = "F:\\Code\\yibayiyi\\20230201_1811_upperpc\\ZTTMS_Manage_yibayiyi_KeepaliveTest3_GKJ_V1.2\\ZTTMS_Manage_yibayiyi_20230320\\bin\\Debug\\ZTTMS_Manage_yibayiyi_20230320.exe";
|
||||
|
||||
//通过exe的允许路径 查找对应的进程
|
||||
private Process FindMainProcessByPath(string exePath)
|
||||
{
|
||||
Process target = null;
|
||||
int portInUseTime = 0;
|
||||
|
||||
foreach (var p in Process.GetProcesses())
|
||||
|
||||
//判断端口是否被占用
|
||||
public static bool PortInUse(int port)
|
||||
{
|
||||
try
|
||||
bool inUse = false;
|
||||
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
|
||||
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
|
||||
foreach (IPEndPoint endPoint in ipEndPoints)
|
||||
{
|
||||
if (p.MainModule.FileName == exePath)
|
||||
if (endPoint.Port == port)
|
||||
{
|
||||
if (p.Id != Process.GetCurrentProcess().Id)
|
||||
{
|
||||
target = p;
|
||||
inUse = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
return target;
|
||||
return inUse; // 返回true说明端口被占用
|
||||
}
|
||||
|
||||
|
||||
@ -107,36 +122,69 @@ namespace MonitoringProgram
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void timerCheckProcessState_Tick(object sender, EventArgs e)
|
||||
{
|
||||
Process process = null;
|
||||
Process[] process = null;
|
||||
try
|
||||
{
|
||||
// 获取指定ID的进程
|
||||
process = Process.GetProcessById(targetProcessId);
|
||||
//process = Process.GetProcessById(targetProcessId);
|
||||
process = Process.GetProcessesByName("ZTTMS_Manage_yibayiyi_20230320");
|
||||
}
|
||||
catch { }
|
||||
|
||||
if (process == null)
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("进程不存在或已退出,需要重启!");
|
||||
Console.WriteLine("GetProcessByIdError:" + ex.Message);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (process == null || process.Length == 0)
|
||||
{
|
||||
Console.WriteLine("进程不存在或已退出,开始监测端口是否被占用!");
|
||||
|
||||
bool isUse = PortInUse(55000);
|
||||
Console.WriteLine("端口占用情况:" + isUse.ToString());
|
||||
if (isUse)
|
||||
{
|
||||
//端口被占用
|
||||
portInUseTime++;
|
||||
|
||||
if (portInUseTime > 4)
|
||||
{
|
||||
//重启工控机,30s后重启
|
||||
Process.Start("shutdown", "/r /t 30"); // 参数 /r 的意思是要重新启动计算机
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//端口未被占用
|
||||
portInUseTime = 0;
|
||||
|
||||
// 进程不存在 已退出 ,需要重启
|
||||
// 重启代码
|
||||
string exePath = "F:\\Code\\yibayiyi\\ZTTMS_Manage_yibayiyi_KeepaliveTest3_DN_V1.0\\ZTTMS_Manage_yibayiyi_20230320\\bin\\Debug\\ZTTMS_Manage_yibayiyi_20230320.exe";
|
||||
|
||||
var target = Process.Start(exePath, "auto");
|
||||
targetProcessId = target.Id;
|
||||
}
|
||||
else if(checkProcess(process)==false)
|
||||
}
|
||||
else if (checkProcess(process[0]) == false)
|
||||
{
|
||||
Console.WriteLine("进程卡死,30后重启计算机!");
|
||||
|
||||
//进程卡死
|
||||
process.Kill();
|
||||
process[0].Kill();
|
||||
|
||||
//重启进程
|
||||
string exePath = "F:\\Code\\yibayiyi\\ZTTMS_Manage_yibayiyi_KeepaliveTest3_DN_V1.0\\ZTTMS_Manage_yibayiyi_20230320\\bin\\Debug\\ZTTMS_Manage_yibayiyi_20230320.exe";
|
||||
|
||||
var target = Process.Start(exePath, "auto");
|
||||
targetProcessId = target.Id;
|
||||
//重启工控机,30s后重启
|
||||
Process.Start("shutdown", "/r /t 30"); // 参数 /r 的意思是要重新启动计算机
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("进程继续运行!");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("CheckProcessStateError:" + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -9,3 +9,14 @@ F:\Code\yibayiyi\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram
|
||||
F:\Code\yibayiyi\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.exe
|
||||
F:\Code\yibayiyi\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.pdb
|
||||
F:\Code\yibayiyi\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.Form1.resources
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\bin\Debug\MonitoringProgram.exe.config
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\bin\Debug\MonitoringProgram.exe
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\bin\Debug\MonitoringProgram.pdb
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.csproj.AssemblyReference.cache
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.csproj.SuggestedBindingRedirects.cache
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.Form1.resources
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.Properties.Resources.resources
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.csproj.GenerateResource.cache
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.csproj.CoreCompileInputs.cache
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.exe
|
||||
F:\Code\yibayiyi\20230201_1811_upperpc_Monitor\MonitoringProgram\MonitoringProgram\obj\Debug\MonitoringProgram.pdb
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user