title: check if app is running already under current user
body: <p>In a terminal server situation I want to make sure only 1 instance of the application runs for each user.</p>
  
  <p>What is the best way to do this?</p>
  
  <p>Here is what i'm currently using but it seems like it isn't working as intended. I can't be 100% on that.</p>
  
  <pre><code>int SID = Process.GetCurrentProcess().SessionId;
  
  Process current_process = Process.GetCurrentProcess();
  int proc_count = 0;
  
  foreach (Process p in Process.GetProcessesByName(current_process.ProcessName))
  {
      proc_count++;
      if (p.SessionId.Equals(SID))
      {
          if (proc_count &gt; 1)
          {
              Application.Current.Shutdown();
              return;
          }
      }
  }
  </code></pre>
  
  <p><em><strong>UPDATE</em></strong>
  To help clarify by the comments I have read I think it needs to be off the session ID because not all clients will follow one physical person per username and there for the user can be logged into the server multiple times.</p>