wpf异常,急求教什么是WPF问题?

1、在App.xaml.cs文件中添加以下代码
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
/// <summary>
/// 设置程序单例运行
/// </summary>
private static Mutex mutex;
protected override void OnStartup(StartupEventArgs e)
{
mutex = new Mutex(true, "Channel", out bool ret);
if (!ret)
{
MessageBox.Show("程序已经打开");
Environment.Exit(0);
}
base.OnStartup(e);
RegisterEvents();
}
/// <summary>
/// 注册事件
/// </summary>
private void RegisterEvents()
{
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
//UI线程未捕获异常处理事件(UI主线程)
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
try
{
var exception = e.Exception as Exception;
if (exception != null)
{
HandleException(exception);
}
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
}
}
//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
var exception = e.ExceptionObject as Exception;
if (exception != null)
{
HandleException(exception);
}
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
//ignore
}
}
//UI线程未捕获异常处理事件(UI主线程)
private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
try
{
HandleException(e.Exception);
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
//处理完后,我们需要将Handler=true表示已此异常已处理过
e.Handled = true;
}
}
private static void HandleException(Exception e)
{
//将异常信息写入到日志文件
FileUtil.Log(e.ToString());
}
}2、日志文件操作
class FileUtil
{
public string filePath;
// #region API函数声明
[DllImport("kernel32")]//返回0表示失败,非0为成功
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
//
#endregion
public FileUtil(string filePath)
{
if (File.Exists(filePath))
{
this.filePath = filePath;
}
else
{
FileStream fs = File.Create(filePath);
fs.Close();
this.filePath = filePath;
}
}
/// <summary>
/// 读
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <returns></returns>
public string Read(string section, string key, string def)
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, temp, 1024, filePath);
return temp.ToString();
}
/// <summary>
/// 写
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool Write(string section, string key, string value)
{
long OpStation = WritePrivateProfileString(section, key, value, filePath);
if (OpStation == 0)
{
return false;
}
return true;
}
/// <summary>
/// 写入日志
/// </summary>
/// <param name="text"></param>
public static void Log(string text)
{
string[] nowRes = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Split(new char[] { ' ' });
string logFile = @"Log\" + nowRes[0] + ".log";
if (!Directory.Exists("Log"))
{
Directory.CreateDirectory("Log");
}
File.AppendAllText(logFile, "[" + nowRes[1] + "] " + text + "\r\n");
}
}
}
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
private const string Tag = nameof(App);
public App()
{
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
Logger.Fatal(Tag,"",e.Exception);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
var terminatingMessage = e.IsTerminating ? " The application is terminating." : string.Empty;
var exceptionMessage = exception?.Message ?? "An unmanaged exception occured.";
var message = string.Concat(exceptionMessage, terminatingMessage);
Logger.Fatal(Tag, message, exception);
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
Logger.Fatal(Tag, "", e.Exception);
}
}--------------------------------------------------------------------------------------------------------------------------------------------------}

我要回帖

更多关于 什么是WPF 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信