Skip to content

Instantly share code, notes, and snippets.

@Larry57
Created November 26, 2012 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Larry57/4147612 to your computer and use it in GitHub Desktop.
Save Larry57/4147612 to your computer and use it in GitHub Desktop.
Force a single instance of an application by remote desktop session
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