2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="AppRestart.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-03-17 09:05:52 +13:00
|
|
|
|
|
2020-03-28 12:42:32 +13:00
|
|
|
|
namespace SystemTrayMenu.Utilities
|
2020-03-17 09:05:52 +13:00
|
|
|
|
{
|
2022-10-21 04:55:54 +13:00
|
|
|
|
using System;
|
2021-05-09 22:18:19 +12:00
|
|
|
|
using System.ComponentModel;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Windows.Forms;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2020-03-17 09:05:52 +13:00
|
|
|
|
internal class AppRestart
|
|
|
|
|
{
|
2022-10-21 04:55:54 +13:00
|
|
|
|
public static event Action BeforeRestarting;
|
2020-03-17 09:05:52 +13:00
|
|
|
|
|
2020-07-08 03:05:19 +12:00
|
|
|
|
internal static void ByThreadException()
|
|
|
|
|
{
|
|
|
|
|
Restart(GetCurrentMethod());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 05:24:01 +13:00
|
|
|
|
internal static void ByAppContextMenu()
|
2020-07-08 03:05:19 +12:00
|
|
|
|
{
|
|
|
|
|
Restart(GetCurrentMethod());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void ByConfigChange()
|
|
|
|
|
{
|
|
|
|
|
Restart(GetCurrentMethod());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 05:24:01 +13:00
|
|
|
|
internal static void ByMenuButton()
|
|
|
|
|
{
|
|
|
|
|
Restart(GetCurrentMethod());
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 00:32:47 +13:00
|
|
|
|
private static void Restart(string reason)
|
2020-03-17 09:05:52 +13:00
|
|
|
|
{
|
|
|
|
|
BeforeRestarting?.Invoke();
|
|
|
|
|
Log.Info($"Restart by '{reason}'");
|
|
|
|
|
Log.Close();
|
2020-06-06 10:59:01 +12:00
|
|
|
|
|
2021-11-17 12:13:46 +13:00
|
|
|
|
using (Process p = new())
|
2020-06-06 10:59:01 +12:00
|
|
|
|
{
|
2022-02-21 05:22:34 +13:00
|
|
|
|
string fileName = System.Environment.ProcessPath;
|
2022-01-08 06:24:17 +13:00
|
|
|
|
p.StartInfo = new ProcessStartInfo(fileName);
|
|
|
|
|
|
2021-05-09 22:18:19 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
p.Start();
|
|
|
|
|
}
|
|
|
|
|
catch (Win32Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn("Restart failed", ex);
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
Application.Exit();
|
2020-03-17 09:05:52 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2020-03-24 00:32:47 +13:00
|
|
|
|
private static string GetCurrentMethod()
|
2020-03-17 09:05:52 +13:00
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
StackTrace st = new();
|
2020-03-24 00:38:23 +13:00
|
|
|
|
StackFrame sf = st.GetFrame(1);
|
2020-03-17 09:05:52 +13:00
|
|
|
|
|
|
|
|
|
return sf.GetMethod().Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|