Created
December 12, 2012 13:40
-
-
Save avlee/4267790 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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