From 199c204b5d76b57446067d35b46b80b9bdf94031 Mon Sep 17 00:00:00 2001 From: Syrone Wong Date: Thu, 10 Nov 2016 07:56:23 +0800 Subject: [PATCH] Misc - detach event handler before exiting - eliminate UI and non-UI exception handling race - rework exception message Signed-off-by: Syrone Wong --- shadowsocks-csharp/Program.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 198fbd5c..5554861d 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -92,9 +92,10 @@ namespace Shadowsocks { if (Interlocked.Increment(ref exited) == 1) { - Logging.Error(e.ExceptionObject?.ToString()); + string errMsg = e.ExceptionObject.ToString(); + Logging.Error(errMsg); MessageBox.Show( - $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{e.ExceptionObject?.ToString()}", + $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{errMsg}", "Shadowsocks non-UI Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } @@ -102,12 +103,15 @@ namespace Shadowsocks private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { - string errorMsg = $"Exception Type: {e.Exception.GetType().Name}{Environment.NewLine}Stack Trace:{Environment.NewLine}{e.Exception.StackTrace}"; - Logging.Error(errorMsg); - MessageBox.Show( - $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{errorMsg}", - "Shadowsocks UI Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - Application.Exit(); + if (Interlocked.Increment(ref exited) == 1) + { + string errorMsg = $"Exception Detail: {Environment.NewLine}{e.Exception}"; + Logging.Error(errorMsg); + MessageBox.Show( + $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{errorMsg}", + "Shadowsocks UI Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Application.Exit(); + } } private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) @@ -169,6 +173,7 @@ namespace Shadowsocks private static void Application_ApplicationExit(object sender, EventArgs e) { SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; + Application.ThreadException -= Application_ThreadException; HotKeys.Destroy(); if (_controller != null) {