SystemTrayMenu/Utilities/AppRestart.cs

71 lines
1.7 KiB
C#
Raw Permalink Normal View History

// <copyright file="AppRestart.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
2020-03-17 09:05:52 +13:00
namespace SystemTrayMenu.Utilities
2020-03-17 09:05:52 +13:00
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
2020-03-17 09:05:52 +13:00
internal class AppRestart
{
public static event Action BeforeRestarting;
2020-03-17 09:05:52 +13:00
internal static void ByThreadException()
{
Restart(GetCurrentMethod());
}
internal static void ByAppContextMenu()
{
Restart(GetCurrentMethod());
}
internal static void ByConfigChange()
{
Restart(GetCurrentMethod());
}
internal static void ByMenuButton()
{
Restart(GetCurrentMethod());
}
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
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;
p.StartInfo = new ProcessStartInfo(fileName);
try
{
p.Start();
}
catch (Win32Exception ex)
{
Log.Warn("Restart failed", ex);
}
}
Application.Exit();
2020-03-17 09:05:52 +13:00
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static string GetCurrentMethod()
2020-03-17 09:05:52 +13:00
{
StackTrace st = new();
StackFrame sf = st.GetFrame(1);
2020-03-17 09:05:52 +13:00
return sf.GetMethod().Name;
}
}
}