SystemTrayMenu/Utilities/AppRestart.cs
2020-06-06 00:59:01 +02:00

61 lines
1.6 KiB
C#

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace SystemTrayMenu.Utilities
{
internal class AppRestart
{
public static event EventHandlerEmpty BeforeRestarting;
private static void Restart(string reason)
{
BeforeRestarting?.Invoke();
Log.Info($"Restart by '{reason}'");
Log.Close();
using (Process p = new Process())
{
string fileName = Assembly.GetExecutingAssembly().
ManifestModule.FullyQualifiedName.Replace(
"SystemTrayMenu.dll", "SystemTrayMenu.exe",
StringComparison.OrdinalIgnoreCase);
p.StartInfo = new ProcessStartInfo(fileName);
p.Start();
};
Application.Exit();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static string GetCurrentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
internal static void ByThreadException()
{
Restart(GetCurrentMethod());
}
internal static void ByMenuNotifyIcon()
{
Restart(GetCurrentMethod());
}
internal static void ByDisplaySettings(object sender, EventArgs e)
{
Restart(GetCurrentMethod());
}
internal static void ByConfigChange()
{
Restart(GetCurrentMethod());
}
}
}