WaitforExit doesn't work correctly

后端 未结 4 2038
粉色の甜心
粉色の甜心 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:57

    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.
    }
    
    0 讨论(0)
  • 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);  
      }
      
    0 讨论(0)
  • 2021-01-21 03:05

    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.

    0 讨论(0)
  • 2021-01-21 03:07

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

    0 讨论(0)
提交回复
热议问题