using System;
using System.Diagnostics;
using System.Windows;
namespace Artemis.Core
{
///
/// Provides a few general utilities for ease of use
///
public static class Utilities
{
///
/// Attempts to gracefully shut down the application with a delayed kill to ensure the application shut down
///
/// This is required because not all SDKs shut down properly, it is too unpredictable to just assume we can
/// gracefully shut down
///
///
/// The delay in seconds after which to kill the application (ignored when a debugger is attached)
/// Whether or not to restart the application after shutdown (ignored when a debugger is attached)
public static void Shutdown(int delay, bool restart)
{
// Always kill the process after the delay has passed, with all the plugins a graceful shutdown cannot be guaranteed
string arguments = "-Command \"& {Start-Sleep -s " + delay + "; (Get-Process 'Artemis.UI').kill()}";
// If restart is required, start the executable again after the process was killed
if (restart)
arguments = "-Command \"& {Start-Sleep -s " + delay + "; (Get-Process 'Artemis.UI').kill(); Start-Process -FilePath '" + Process.GetCurrentProcess().MainModule.FileName + "'}\"";
ProcessStartInfo info = new ProcessStartInfo
{
Arguments = arguments,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
FileName = "PowerShell.exe"
};
if (!Debugger.IsAttached)
Process.Start(info);
// Request a graceful shutdown, whatever UI we're running can pick this up
OnShutdownRequested();
}
///
/// Opens the provided URL in the default web browser
///
/// The URL to open
/// The process created to open the URL
public static Process OpenUrl(string url)
{
ProcessStartInfo processInfo = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
return Process.Start(processInfo);
}
///
/// Gets the current application location
///
///
internal static string GetCurrentLocation()
{
return Process.GetCurrentProcess().MainModule.FileName;
}
#region Events
public static event EventHandler ShutdownRequested;
private static void OnShutdownRequested()
{
ShutdownRequested?.Invoke(null, EventArgs.Empty);
}
#endregion
}
}