title: Executing commands from C# problem
body: <p>I've made a little C# tool for monitoring sidebar.exe (as some know there's a strange memory leak in it, so this monitor restarts the sidebar.exe process when it reaches a limit...).</p>
  
  <p>Before this I had a .bat file doing it and I was running it manually, and it worked fine. Now, I'm executing the cmd commands from C#, but the behavior is different: only 4 of the 7 gadgets appear again after "start sidebar.exe". It always happens with the program, and never when run manually.</p>
  
  <p>What's the difference in running the commands as .bat or from the process? And can I fix this?</p>
  
  <p>I also tried running the .bat itself from the process, same problem...</p>
  
  <p>The commands are:</p>
  
  <p><code>taskkill /im sidebar.exe /f</code></p>
  
  <p><code>TIMEOUT /T 2</code> in .bat or <code>System.Threading.Thread.Sleep(2000);</code> in program</p>
  
  <p><code>start sidebar.exe</code></p>
  
  <p>Update: The code of what I tried:<br>
  Option 1 - run .bat: <code>System.Diagnostics.Process.Start(@"C:\....restart.bat");</code><br>
  Option 2 - run commands:<br>
  <code>...</code><br>
  <code>this.executeCmd("taskkill /im sidebar.exe /f");</code><br>
  <code>System.Threading.Thread.Sleep(2000);</code><br>
  <code>this.executeCmd("start sidebar.exe");</code><br>
  <code>...</code></p>
  
  <pre><code>private void executeCmd(String command)
      {
          ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
          psi.RedirectStandardOutput = true;
          psi.UseShellExecute = false;
          psi.CreateNoWindow = true;
          System.Diagnostics.Process proc = new System.Diagnostics.Process();
          proc.StartInfo = psi;
          proc.Start();
          string result = proc.StandardOutput.ReadToEnd();
          System.Console.Out.WriteLine(result);
      }
  </code></pre>
  
  <p>Update:<br>
  I've been told it's some permissions/account/user issue, tried to solve it with no success, but I noticed that in Java this doesn't happen so I combined Java and C# for this tool (a java runnable called on startup, using a small exe which returns the usage of sidebar.exe) - works perfect. Still would appreciate an answer if anyone knows about this C# issue though.</p>