WaitforExit doesn't work correctly

风格不统一 提交于 2019-12-01 22:25:40

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);  
    }
    

I may be totally wrong here, but...

process1.StartInfo.Arguments = "-Xmx512M -jar";
process1.StartInfo.Arguments += toLoad;

You need to have a space after the -jar

Or else Java is going to immediately bomb out.

Trying moving the WaitForExit call out of the UI thread.

As @M. Babcock mentioned, you are holding up updates to the rich text box.

A solution similar to this may work:

Modify btnLoad_Click to start a new thread that processes the list (this goes in the count > 0 branch)

Thread thread = new Thread(new ThreadStart(LoadPbThread));
thread.Start();

Then, add this:

void LoadPbThread()
{
    String toLoad;
    for (int i=0; i < count;i++)
    {//START OF FOREACH
        toLoad = lstBarToLoad.Items[i].Text;
        //call load method.
        loaddPB(toLoad);      
     }//end of for.
}

As it is adviced here, setting process.EnableRaisingEvents = true solved the problem of the blocked ui in my case.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!