Skip to content

Instantly share code, notes, and snippets.

@peters
Last active December 22, 2015 19:18
Show Gist options
  • Save peters/6518385 to your computer and use it in GitHub Desktop.
Save peters/6518385 to your computer and use it in GitHub Desktop.
Restart your application after an successfull update. Shimmer.Client -> Application.cs Shimmer.Client.RestartApplication -> Program.cs (Create new console project)
using System.Diagnostics;
using System.Reflection;
namespace Shimmer.Client
{
public static class Application
{
/// <summary>
/// Restart an application.
/// </summary>
/// <param name="assembly">Assembly to restart.</param>
/// <param name="commandlineSwitches">Additional commandline switches that should
/// be passed to your application during startup.</param>
public static void Restart(Assembly assembly, string commandlineSwitches = null)
{
Process.Start(typeof(RestartApplication.AssemblyRef).Assembly.Location,
string.Format("--pid={0} --filename={1} --arguments={2}", Process.GetCurrentProcess().Id, assembly.Location, commandlineSwitches))
.WaitForInputIdle();
System.Windows.Application.Current.Shutdown();
}
}
}
using System.Diagnostics;
namespace Shimmer.Client.RestartApplication
{
class Program
{
static void Main(string[] args)
{
if (args == null || args.Length != 3) return;
var targetPid = int.Parse(GetValue("--pid=", args[0]));
if (!Process.GetProcessById(targetPid).WaitForExit(15 * 1000)) {
return;
}
Process.Start(new ProcessStartInfo
{
FileName = GetValue("--filename=", args[1]),
Arguments = GetValue("--arguments=", args[2]),
WindowStyle = ProcessWindowStyle.Hidden
});
}
private static string GetValue(string argument, string segment)
{
return segment.Length > argument.Length ? segment.Substring(argument.Length) : string.Empty;
}
}
public abstract class AssemblyRef {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment