using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace supervi
{
class Program
{
/// <summary>
///
/// </summary>
/// <param name="args">
/// 1- 监测的目录,没有文件创建将执行第3个参数命令
/// 2- 可以容忍超过多久后重启程序,单位是分钟,监测周期40秒钟进行一次监测;
/// 3- 重启的程序;
/// </param>
static void Main(string[] args)
{
//Program P = new Program(@"D:answer",@"c:windowssystem32calc.exe");
//@"D:answer"
if (args.Length > 3)
{
Program P = new Program(args[0], args[2],args[3]);
Console.WriteLine("回车结束程序……");
P.start(args[1]);
Console.ReadLine();
}
}
//最后创建文件的时间;
DateTime _x = DateTime.Now;
public Program() { }
/// <summary>
///
/// </summary>
/// <param name="watcherDir">需要进行文件写入监控的目录</param>
/// <param name="ProcFilename">根据监控结果决定可能需要重新启动的应用程序</param>
public Program(string watcherDir,string ProcFilename,string args)
{
System.IO.FileSystemWatcher fw = new System.IO.FileSystemWatcher(watcherDir);
fw.Created += fw_Created;
fw.EnableRaisingEvents = true;
proces = new System.Diagnostics.Process();
proces.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(ProcFilename);
proces.StartInfo.FileName = System.IO.Path.GetFileName(ProcFilename);
proces.StartInfo.Arguments = args;
proces.Start();
}
System.Diagnostics.Process proces;
int counter;
void run(object obj)
{
DateTime dt = DateTime.Now;
Console.WriteLine("run ..." + dt.ToString() + "..." + dt.Millicond.ToString().PadLeft(3, '0') +
"|LastWriteTime : " + _x.ToString() + "..." + _x.Millicond.ToString().PadLeft(3, '0') + "|" + obj.ToString());
TimeSpan ts = DateTime.Now - _x;
if (ts.Minutes > Convert.ToInt32(obj))
{
proces.Kill();
System.Threading.Thread.Sleep(2000);
//推迟判断依据,因为已经重新启动程序,防止连续重启程序;
_x = DateTime.Now;
if (counter >= 5)
{
logwrite("RestartWindows", @"Process start > 5...");
System.Diagnostics.Process.Start("shutdown.exe", "-r -f");
return;
}
counter++;
proces.Start();
logwrite("ProcessTimeout", "Process have restart...");
}
}
internal bool logwrite(string lpad, string content)
{
System.IO.File.AppendAllText(
logfilename(lpad)
,
"时间:" + DateTime.Now.ToString() + "|||" + content + " "
);
return true;
}
private string logfilename(string lp)
{
if (!System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath + @"log"))
System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + @"log");
return
System.Windows.Forms.Application.StartupPath + @"log" + lp + "_" +
DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString().PadLeft(2, '0') +
DateTime.Now.Day.ToString().PadLeft(2, '0') +
DateTime.Now.Hour.ToString().PadLeft(2, '0') +
".TXT";
}
System.Threading.Timer timer;
void start(object minutes)
{
timer = new System.Threading.Timer(new System.Threading.TimerCallback(run), minutes, 0, 40000);
}
void fw_Created(object nder, System.IO.FileSystemEventArgs e)
{
_x = DateTime.Now;
}
}
}
本文发布于:2023-02-28 20:05:00,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/167765505076075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:directory.createdirectory.doc
本文 PDF 下载地址:directory.createdirectory.pdf
留言与评论(共有 0 条评论) |