进程守护
程序员文章站
2022-05-18 14:29:33
下载地址: 链接:https://pan.baidu.com/s/1xbLyWmst29lIN9ss43PlpA 提取码:0dlf 使用方法:将要守护的.exe文件拖入即可 using FY; using System; using System.Collections.Generic; using ......
下载地址:
链接:https://pan.baidu.com/s/1xblywmst29lin9ss43plpa
提取码:0dlf
使用方法:
将要守护的.exe文件拖入即可
using fy;
using system;
using system.collections.generic;
using system.diagnostics;
using system.io;
using system.text;
using system.threading;
using system.windows.forms;
namespace daemonwf
{
public partial class form1 : form
{
list<processmodel> list = new list<processmodel>();
contextmenustrip strip = new contextmenustrip();
public form1()
{
initializecomponent();
}
private void form1_load(object sender, eventargs e)
{
this.listview1.columns.add("路径", 400, horizontalalignment.left); //一步添加
this.listview1.columns.add("状态", 80, horizontalalignment.left); //一步添加
listview1.view = view.details;
strip.click += items1_click;
readtxt();
//程序处理
thread thread1 = new thread(new threadstart(restart));
thread1.start();
}
private void form1_formclosed(object sender, formclosedeventargs e)
{
environment.exit(0);
}
/// <summary>
/// 程序处理
/// </summary>
public void restart()
{
while (true)
{
try
{
if (list.count > 0)
{
for (int i = 0; i < list.count; i++)
{
string exename = list[i].path.split('\\')[list[i].path.split('\\').length - 1].split('.')[0];
if (list[i].state == 1)
{
try
{
if (process.getprocessesbyname(exename).length < 1)
{
settext(list[i], 5);
process.start(list[i].path);
}
settext(list[i], 6);
thread.sleep(100);
}
catch (exception ex)
{
if (ex.tostring().contains("系统找不到指定的文件"))
{
messagebox.show("找不到指定的文件:" + list[i].path + ",请检查路径的准确性", "消息", messageboxbuttons.ok, messageboxicon.exclamation);
list[i].state = 8;
settext(list[i], 8);
}
else
{
messagebox.show(ex.tostring(), "消息", messageboxbuttons.ok, messageboxicon.exclamation);
}
}
}
if (list[i].state == 3)
{
process[] processes = process.getprocesses();
foreach (process p in processes)
{
if (p.processname == exename)
{
p.kill();
}
settext(list[i], 7);
}
if (process.getprocessesbyname(exename).length < 1)
{
settext(list[i], 7);
}
}
}
}
}
catch (exception ex)
{
if (ex.tostring().contains("系统找不到指定的文件"))
{
messagebox.show("系统找不到指定的文件,请检查路径的准确性", "消息", messageboxbuttons.ok, messageboxicon.exclamation);
}
else
{
messagebox.show(ex.tostring(), "消息", messageboxbuttons.ok, messageboxicon.exclamation);
}
}
thread.sleep(3000);
}
}
private void form1_dragdrop(object sender, drageventargs e)
{
string path = ((system.array)e.data.getdata(dataformats.filedrop)).getvalue(0).tostring();
if (path.split('.')[path.split('.').length - 1].toupper() != "exe")
{
messagebox.show(path + "不是可执行程序");
return;
}
for (int i = 0; i < list.count; i++)
{
if (list[i].path == path)
{
messagebox.show(path + "已存在!");
return;
}
}
list.add(new processmodel()
{
path = path,
state = 1
});
this.listview1.beginupdate(); //数据更新,ui暂时挂起,直到endupdate绘制控件,可以有效避免闪烁并大大提高加载速度
listviewitem lvi = new listviewitem();
lvi.text = path;
lvi.subitems.add("正在启动");
this.listview1.items.add(lvi);
this.listview1.endupdate(); //结束数据处理,ui界面一次性绘制。
}
private void form1_dragenter(object sender, drageventargs e)
{
if (e.data.getdatapresent(dataformats.filedrop))
e.effect = dragdropeffects.link;
else e.effect = dragdropeffects.none;
}
private void listview1_mouseclick(object sender, mouseeventargs e)
{
if (e.button == mousebuttons.right)
{
if (true)//看看是否过滤当前状态
{
string state = listview1.focuseditem.subitems[1].text;
strip.items.clear();
if (state == "启动成功" || state == "正在启动" || state == "开始监控")
{
strip.items.add("停止监控");
strip.items.add("关闭进程");
strip.items.add("删除监控");
}
else if (state == "停止监控")
{
strip.items.add("开始监控");
strip.items.add("关闭进程");
strip.items.add("删除监控");
}
else if (state == "正在关闭" || state == "关闭成功")
{
strip.items.add("开始监控");
strip.items.add("删除监控");
}
else if (state == "启动失败")
{
strip.items.add("开始监控");
strip.items.add("删除监控");
}
}
strip.show(listview1, e.location);//鼠标右键按下弹出菜单
}
}
private void items1_click(object sender, eventargs e)
{
foreach (toolstripitem items in strip.items)
{
if (items.selected == true)
{
switch (items.text)
{
case "开始监控": listview1select(1); break;
case "停止监控": listview1select(2); break;
case "关闭进程": listview1select(3); break;
case "删除监控": listview1select(4); break;
default:
break;
}
}
}
}
public void listview1select(int op)
{
string path = listview1.focuseditem.subitems[0].text;
listview1.focuseditem.subitems[1].text = getstate(op);
for (int i = 0; i < list.count; i++)
{
if (list[i].path == path)
{
if (op == 4)
{
listview1.items.remove(listview1.focuseditem);
list.remove(list[i]);
}
else
{
list[i].state = op;
if (op == 1)
{
listview1.focuseditem.subitems[1].text = getstate(5);
}
}
}
}
}
public string getstate(int op)
{
switch (op)
{
case 1: return "开始监控";
case 2: return "停止监控";
case 3: return "正在关闭";
case 4: return "删除监控";
case 5: return "正在启动";
case 6: return "启动成功";
case 7: return "关闭成功";
case 8: return "启动失败";
default: return "op码有误";
}
}
public void settext(processmodel model, int op)
{
this.begininvoke(new action(() =>
{
try
{
for (int j = 0; j < listview1.items.count; j++)
{
if (listview1.items[j].subitems[0].text == model.path)
{
listview1.items[j].subitems[1].text = getstate(op);
}
}
}
catch (exception ex)
{
rbilogs.writelog("error", ex.tostring());
}
}));
}
/// <summary>
/// 读取历史监控记录
/// </summary>
public void readtxt()
{
string path = appdomain.currentdomain.setupinformation.applicationbase + "logs\\" + "fy.txt";
streamreader sr = new streamreader(path, encoding.default);
string line;
while ((line = sr.readline()) != null)
{
if (!string.isnullorempty(line))
{
list.add(new processmodel()
{
path = line,
state = 1
});
this.listview1.beginupdate(); //数据更新,ui暂时挂起,直到endupdate绘制控件,可以有效避免闪烁并大大提高加载速度
listviewitem lvi = new listviewitem();
lvi.text = line;
lvi.subitems.add("正在启动");
this.listview1.items.add(lvi);
this.listview1.endupdate(); //结束数据处理,ui界面一次性绘制。
}
}
sr.close();
}
/// <summary>
/// 关闭程序记录监控程序路径
/// </summary>
public void writefy()
{
string path = appdomain.currentdomain.setupinformation.applicationbase + "logs\\" + "fy.txt";
filestream fs = new filestream(path, filemode.truncate, fileaccess.readwrite);
fs.close();
for (int i = 0; i < list.count; i++)
{
rbilogs.writelog2(list[i].path);
}
}
/// <summary>
/// 添加双击托盘图标事件(双击显示窗口)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyicon1_mousedoubleclick(object sender, mouseeventargs e)
{
if (windowstate == formwindowstate.minimized)
{
//还原窗体显示
windowstate = formwindowstate.normal;
//激活窗体并给予它焦点
this.activate();
//任务栏区显示图标
this.showintaskbar = true;
//托盘区图标隐藏
notifyicon1.visible = false;
}
}
/// <summary>
/// 判断是否最小化,然后显示托盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void form1_sizechanged(object sender, eventargs e)
{
//判断是否选择的是最小化按钮
if (windowstate == formwindowstate.minimized)
{
//隐藏任务栏区图标
this.showintaskbar = false;
//图标显示在托盘区
notifyicon1.visible = true;
}
}
/// <summary>
/// 确认是否退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void form1_formclosing(object sender, formclosingeventargs e)
{
if (messagebox.show("是否确认退出程序?", "退出", messageboxbuttons.okcancel, messageboxicon.question) == dialogresult.ok)
{
writefy();
// 关闭所有的线程
this.dispose();
this.close();
environment.exit(0);
}
else
{
e.cancel = true;
}
}
/// <summary>
/// 托盘右键显示主界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 显示toolstripmenuitem_click(object sender, eventargs e)
{
windowstate = formwindowstate.normal;
}
/// <summary>
/// 托盘右键退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 退出toolstripmenuitem_click(object sender, eventargs e)
{
if (messagebox.show("是否确认退出程序?", "退出", messageboxbuttons.okcancel, messageboxicon.question) == dialogresult.ok)
{
writefy();
// 关闭所有的线程
this.dispose();
this.close();
environment.exit(0);
}
}
}
}
/// <summary>
/// 进程处理model
/// </summary>
public class processmodel
{
public string path { get; set; }
// 1开始监控 2停止监控 3关闭进程 4删除监控 5正在启动 6启动成功 7关闭成功
public int state { get; set; }
}
推荐阅读