當前位置:
首頁 > 知識 > 非託管異常處理方法

非託管異常處理方法

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: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.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 + "
"

);

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;

}

}

}

非託管異常處理方法

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

Paint API之 Xfermode與PorterDuff詳解
Faster-RCNN訓練自己的數據集

TAG:程序員小新人學習 |