加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
QuartzWork.cs 3.16 KB
一键复制 编辑 原始数据 按行查看 历史
guangbo 提交于 2024-11-06 16:38 . 进程间通讯
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace StudentSynchronization.Daemon
{
/// <summary>
/// 周期任务
/// </summary>
public class QuartzWork
{
//0 0/1 * * * ?
public void Start()
{
// 启动后台线程
Thread backgroundThread = new Thread(StartBackgroundThread);
backgroundThread.IsBackground = true; // 设置为后台线程
backgroundThread.Start();
}
public static WinProcess winpro = new WinProcess();
private static void StartBackgroundThread()
{
try
{
// 保持后台线程运行
while (true)
{
ProcessData.processlist.Where(p => p.IsRestart).ToList().ForEach(pro =>
{
Task.Run(() =>
{
try
{
var profullname = pro.Path + Path.DirectorySeparatorChar + pro.Name;
if (File.Exists(profullname))
{
var findpro = Process.GetProcessesByName(pro.Name.Replace(".exe", "")).FirstOrDefault(p => p.MainModule.FileName == pro.Path + Path.DirectorySeparatorChar + pro.Name);
if (findpro == null || findpro.Id == 0)
{
winpro.SetForm(pro.Path + Path.DirectorySeparatorChar + pro.Name, pro.Path, pro.Name);
Thread.Sleep(500);
findpro = Process.GetProcessesByName(pro.Name.Replace(".exe", "")).FirstOrDefault(p => p.MainModule.FileName == pro.Path + Path.DirectorySeparatorChar + pro.Name);
}
if (ProcessData.needwindows == "true")
{
ProLogic.Instance.ReStartPro(pro.IndexNum, findpro.Id, 0);
}
}
else if (pro.Info != "配置错误")
{
pro.Info = "配置错误";
if (ProcessData.needwindows == "true")
{
ProLogic.Instance.UpdatePro(pro.IndexNum);
}
}
}
catch
{
}
});
});
if (float.TryParse(ProcessData.timecron, out float timeout))
Thread.Sleep((int)(timeout * 1000)); // 每秒检查一次
else
Thread.Sleep(5000);
}
}
catch (Exception ex)
{
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化