Note that there are some explanatory texts on larger screens.

plurals
  1. POProcess.Start is blocking
    primarykey
    data
    text
    <p>I'm calling Process.Start, but it blocks the current thread.</p> <pre><code>pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe"); // Start process mProcess = new Process(); mProcess.StartInfo = pInfo; if (mProcess.Start() == false) { Trace.TraceError("Unable to run process {0}."); } </code></pre> <p>Even when the process is closed, the code doesn't respond anymore.</p> <p>But Process.Start is really supposed to block? What's going on?</p> <p><em>(The process start correctly)</em></p> <hr> <pre><code>using System; using System.Diagnostics; using System.Threading; using System.Windows.Forms; namespace Test { class Test { [STAThread] public static void Main() { Thread ServerThread = new Thread(AccepterThread); ServerThread.Start(); Console.WriteLine (" --- Press ENTER to stop service ---"); while (Console.Read() &lt; 0) { Application.DoEvents(); } Console.WriteLine("Done."); } public static void AccepterThread(object data) { bool accepted = false; while (true) { if (accepted == false) { Thread hThread = new Thread(HandlerThread); accepted = true; hThread.Start(); } else Thread.Sleep(100); } } public static void HandlerThread(object data) { ProcessStartInfo pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe"); Console.WriteLine("Starting process."); // Start process Process mProcess = new Process(); mProcess.StartInfo = pInfo; if (mProcess.Start() == false) { Console.WriteLine("Unable to run process."); } Console.WriteLine("Still living..."); } } } </code></pre> <p>Console output is:</p> <blockquote> <p>--- Press ENTER to stop service --- Starting process.</p> </blockquote> <hr> <p>Found it: </p> <p><strong>[STAThread]</strong></p> <p>Makes the Process.Start blocking. I read <a href="https://stackoverflow.com/questions/165316/stathread-and-multithreading">STAThread and Multithreading</a>, but I cannot link the concepts with Process.Start behavior.</p> <p>AFAIK, STAThread is <em>required</em> by Windows.Form. How to workaround this problem when using Windows.Form?</p> <hr> <p><em>News for the hell:</em></p> <p>If I rebuild my application, the <strong>first</strong> time I run application work correctly, but if I stop debugging and restart iy again, the problem araise.</p> <p>The problem is not raised when application is executed without the debugger.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload