Created
November 26, 2012 10:46
-
-
Save Larry57/4147612 to your computer and use it in GitHub Desktop.
Force a single instance of an application by remote desktop session
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
static class Program | |
{ | |
[DllImport("user32.dll")] | |
private static extern bool SetForegroundWindow(IntPtr hWnd); | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
var currentProcess = Process.GetCurrentProcess(); | |
var runningProcess = | |
Process.GetProcesses() | |
.Where( | |
p => | |
{ | |
return | |
p.Id != currentProcess.Id && | |
p.ProcessName.Equals(currentProcess.ProcessName, StringComparison.Ordinal) && | |
p.SessionId == currentProcess.SessionId; | |
} | |
) | |
.FirstOrDefault(); | |
if (runningProcess != null) | |
{ | |
SetForegroundWindow(runningProcess.MainWindowHandle); | |
return; | |
} | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
Application.Run(new TagPickerForm()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment