非托管代码异常

该程序使用FileSystemWatcher监控指定目录,当检测到新文件创建时记录时间,并在超过预设时间分钟后重启指定程序。如果连续多次重启,将执行系统重启操作。同时,程序具备日志记录功能,便于跟踪和调试。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace supervise
{
    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:\windows\system32\calc.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.Millisecond.ToString().PadLeft(3, '0') +
                "|LastWriteTime : " + _x.ToString() + "..." + _x.Millisecond.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 + "\r\n"
                    );
            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 sender, System.IO.FileSystemEventArgs e)
        {
            _x = DateTime.Now;
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值