Search code examples
javaprocessruntime.execswingutilities

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:

    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:

       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(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. 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:

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

Solution

  • you need to call tryDemo.start(), not tryDemo.run(). you are running the thread directly, not spawning a separate invocation.