WaitforExit doesn't work correctly

后端 未结 4 2088
粉色の甜心
粉色の甜心 2021-01-21 02:19

So I have been battling this issue for awhile now and tried many different ways to fix it but cannot.

Bascally waht my app does is calls a java file to load an applica

4条回答
  •  清歌不尽
    2021-01-21 02:59

    If you WaitForExit, your application blocks (waits) until the process exits. This means it is unable to process any Windows messages in its UI thread, so it doesn't update the UI.

    You need to start the process "in the background" so your UI continues to refresh. This can be done with:

    • Start and monitor the process from a separate thread, and pass progress information back to the UI thread for display
    • Add an event handler to the process exited event, or periodically poll the process.HasExited flag, and use this to know when the first process has finished. Your event handler would start this process off and then exit back to your main application loop so that it runs as normal while waiting for the external process to finish.
    • Sit in a busy wait loop until it completes, and process application events. (Beware of this, as any events that cause reentrant calls to this code could do very bad things. Generally if you use this approach you nee dto make sure that the rest of your application is "locked down" in a state where it knows it is busy waiting for a process to complete). THis is effectively what WaitForExit does, but it also processes application events, allowing the UI to remain vaguely responsive:

      while (!process.HasExited)  
      {  
          Application.DoEvents();  
          Thread.Sleep(100);  
      }
      

提交回复
热议问题