非托管异常处理方法有哪些_制造异常处理方法及流程

(1) 2024-08-29 18:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
非托管异常处理方法有哪些_制造异常处理方法及流程,希望能够帮助你!!!。

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;

}

}

}

非托管异常处理方法有哪些_制造异常处理方法及流程_https://bianchenghao6.com/blog__第1张

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复