Skip to content

Instantly share code, notes, and snippets.

@avlee
Created December 12, 2012 13:40
Show Gist options
  • Save avlee/4267790 to your computer and use it in GitHub Desktop.
Save avlee/4267790 to your computer and use it in GitHub Desktop.
public static Process PriorProcess()
// Returns a System.Diagnostics.Process pointing to
// a pre-existing process with the same name as the
// current one, if any; or null if the current process
// is unique.
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) &&
(p.MainModule.FileName == curr.MainModule.FileName))
return p;
}
return null;
}
protected override void PrepareApplication()
{
var priorProcess = PriorProcess();
if (priorProcess != null)
{
ShowWindow(priorProcess.MainWindowHandle, 1);
SetWindowPos(priorProcess.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, 3);
SetForegroundWindow(priorProcess.MainWindowHandle);
Environment.Exit(1);
}
else
{
base.PrepareApplication();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment