純自己記錄
在運行有可能出問題的 語句前,將函數添加進事件
//// 取當前作用域
//AppDomain currentDomain = AppDomain.CurrentDomain;
//// 當前作用域出現未捕獲異常時,使用MyHandler函數響應事件
//currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// 當前作用域出現未捕獲異常時,使用MyHandler函數響應事件
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
try
{
Task taskRestart = Task.Factory.StartNew(() => eventRestart(m_logger,this));
//taskRestart.Start();在上一句eventRestart已經調用了 不需要start再調一遍
return;
}
catch (Exception ex)
{
m_logger.LogError("重新啓動錯誤:"+ ex.ToString());
}
函數本體:
// 函數處理後異常會繼續,不需要重新拋出異常
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (null == ex)
{
m_logger.LogError("兜底處理所有未處理的異常Unhandled Exception");
}
else
{
m_logger.LogError("測試m_logger有沒有問題");
m_logger.LogError("兜底處理所有未處理的異常Unhandled Exception", ex.ToString());
}
}
---------------------------還有一種,只是可以通過狀態看到是否成功 但不知道原因的--------------------------
主要運行是Process.Start(psi),通過看process的狀態
Process process = Process.Start(psi);
StreamReader myStreamReader = process.StandardError;
Thread.Sleep(3000);//停3秒,等那邊開始運行,再獲取狀態 不然還沒運行就拿狀態 沒用
bool isExited = process.HasExited;//遇到錯誤就會關閉/終止 存在就是沒錯
if (isExited)
{
// myStreamReader.Seek(0, SeekOrigin.Current)
Console.WriteLine("打印信息" + myStreamReader.ReadToEnd());
}
Console.WriteLine("process" + process.ToString());