第一次写博客,如有错误,请大家及时告知,本人立即改之。
如果您有好的想法或者建议,我随时与我联系。
如果发现代码有误导时,请与我联系,我立即改之。
好了不多说,直接贴代码。
一般的错误,使用下面两个就可以了。我不太赞同项目里面大量使用try{}catch{}(释放资源除外)
//处理UI线程异常
Application.Current.DispatcherUnhandledException += App_DispatcherUnhandledException;
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += App_CurrentDomainUnhandledException;
public delegate int CallBack(ref long a);
CallBack myCall;
[DllImport("kernel32")]
private static extern Int32 SetUnhandledExceptionFilter(CallBack cb);
myCall = new CallBack(ExceptionFilter);
SetUnhandledExceptionFilter(myCall);
此函数可让程序"美化"结束。
完整代码:
public partial class App : Application
{
public delegate int CallBack(ref long a);
CallBack myCall;
[DllImport("kernel32")]
private static extern Int32 SetUnhandledExceptionFilter(CallBack cb);
Mutex mutex;
public App()
{
Startup += new StartupEventHandler(App_Startup);
}
void App_Startup(object sender, StartupEventArgs e)
{
bool ret;
mutex = new System.Threading.Mutex(true, "{wpf-8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}", out ret);
if (!ret)
{
Environment.Exit(0);
return;
}
Log4net.Init(ty