简体   繁体   English

执行进程时,GUI无法更新(SwingUtilities.invokeLater)

[英]GUI unable to update when executing process (SwingUtilities.invokeLater)

referring to my previous question at Unable to perform any action before Process.Runtime.exec statement line , I have changed my code into two part, a thread class CmdExec that has all code to execute an external program as below: 提到我在Process.Runtime.exec语句行之前无法执行任何操作的上一个问题,我将代码更改为两部分,一个线程类CmdExec,该类具有执行外部程序的所有代码,如下所示:

    public class CmdExec extends Thread
     {
     private String cmd;
     private File path;

      public CmdExec() {
      }

      public CmdExec(String cmd, File path) {
      this.cmd = cmd;
      this.path = path;
      }

      public void run(){

  try
    {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd , null, path);
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<ERROR>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</ERROR>");
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t)
      {
        t.printStackTrace();
      }

and by referring to jtahlborn answer, I have done another Runnable class for GUI update purpose as well, as below: 通过引用jtahlborn的答案,我还完成了另一个用于GUI更新目的的Runnable类,如下所示:

       Runnable doWorkRunnable = new Runnable() {
        public void run() {
System.out.println("hello world");
btnTranscribe.setEnabled(false);
areaOutput.setEditable(false);
areaOutput.setEnabled(false);
areaOutput.setText("Performing segmentation, please wait till process is done\n"); }
        };

I call to SwingUtilities.invokeLater to change the GUI before actually running the process.exec() to call external program as showed below: 我调用SwingUtilities.invokeLater来更改GUI,然后实际运行process.exec()来调用外部程序,如下所示:

    SwingUtilities.invokeLater(doWorkRunnable);
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(cmd , null, path);

but with all codes above, the GUI still failed to update and it is only updated after the process is done. 但是使用上述所有代码,GUI仍然无法更新,并且仅在完成该过程后才更新。 Am I wrong at any particular steps in coordinating these two runnable and threads? 在协调这两个可运行线程和线程的任何特定步骤上,我是否都错了?

Thank in advance for your great help and answer 预先感谢您的大力帮助和答复

P / S: I start execute CmdExec() during a button is pressed in the GUI as below: P / S:我在GUI中按下按钮期间开始执行CmdExec(),如下所示:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     strSegment = "java -Xmx2024m -jar ./LIUM_SpkDiarization-4.2.jar / --fInputMask=" +               strAudioOut + "/%s.wav"
            + " --sOutputMask=" + strCtlOut + "/%s.ctl --sOutputFormat=ctl --            doCEClustering --cMinimumOfCluster=1 new3_20110331103858";

    CmdExec tryDemo = new CmdExec();
    tryDemo = new CmdExec(strSegment, fSegment);
    tryDemo.run();

    strExtract = "./sphinx_fe -i " + strAudioOut + "/new3_20110331103858.wav"
           + " -o " + strFeatureOut + "/new3_20110331103858.mfc";
    //System.out.println (strExtract);
    //executeCommand (strExtract, fExtract);

    tryDemo = new CmdExec(strExtract, fExtract);
    tryDemo.run();
      }

you need to call tryDemo.start(), not tryDemo.run(). 您需要调用tryDemo.start(),而不是tryDemo.run()。 you are running the thread directly, not spawning a separate invocation. 您正在直接运行线程,而不产生单独的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM