非托管异常处理方法

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;
}
}
}
以上就是朝夕生活(www.30zx.com)关于“非托管异常处理方法”的详细内容,希望对大家有所帮助!

猜你喜欢