Handle UnhandledExceptions

This commit is contained in:
MaxXor 2015-09-05 22:25:57 +02:00
parent 5b34f03ae1
commit 5944925c3e
2 changed files with 48 additions and 10 deletions

View File

@ -106,6 +106,29 @@ namespace xClient.Core.Helper
}
}
public static string CreateRestartBatch()
{
try
{
string batchFile = GetTempFilePath(".bat");
string uninstallBatch =
"@echo off" + "\n" +
"echo DONT CLOSE THIS WINDOW!" + "\n" +
"ping -n 10 localhost > nul" + "\n" +
"start \"\" " + "\"" + ClientData.CurrentPath + "\"" + "\n" +
"del " + "\"" + batchFile + "\"";
File.WriteAllText(batchFile, uninstallBatch);
return batchFile;
}
catch (Exception)
{
return string.Empty;
}
}
public static bool ClearReadOnly(string filePath)
{
try

View File

@ -28,6 +28,7 @@ namespace xClient
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
Settings.Initialize();
Initialize();
@ -37,6 +38,24 @@ namespace xClient
Cleanup();
}
private static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.IsTerminating)
{
string batchFile = FileHelper.CreateRestartBatch();
if (string.IsNullOrEmpty(batchFile)) return;
ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
FileName = batchFile
};
Process.Start(startInfo);
Environment.Exit(0);
}
}
private static void Cleanup()
{
CommandHandler.CloseShell();
@ -115,23 +134,19 @@ namespace xClient
private static void Initialize()
{
if (!MutexHelper.CreateMutex(Settings.MUTEX))
ClientData.Disconnect = true; // process with same mutex is already running
_hosts = new HostsManager(HostHelper.GetHostsList(Settings.HOSTS));
// process with same mutex is already running
if (!MutexHelper.CreateMutex(Settings.MUTEX) || _hosts.IsEmpty) // no hosts to connect
ClientData.Disconnect = true;
if (ClientData.Disconnect)
return;
AES.PreHashKey(Settings.PASSWORD);
_hosts = new HostsManager(HostHelper.GetHostsList(Settings.HOSTS));
ClientData.InstallPath = Path.Combine(Settings.DIR, ((!string.IsNullOrEmpty(Settings.SUBFOLDER)) ? Settings.SUBFOLDER + @"\" : "") + Settings.INSTALLNAME);
GeoLocationHelper.Initialize();
if (_hosts.IsEmpty)
ClientData.Disconnect = true; // no hosts to connect
if (ClientData.Disconnect)
return;
FileHelper.DeleteZoneIdentifier(ClientData.CurrentPath);
if (!Settings.INSTALL || ClientData.CurrentPath == ClientData.InstallPath)