2011-04-01 24 views
0

Unable to perform any action before Process.Runtime.exec statement line指我剛纔的問題更新,我已經改變了我的代碼分成兩個部分,其所有的代碼執行如下的外部程序線程類的CmdExec:GUI無法執行過程時(SwingUtilities.invokeLater)

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

並參照jtahlborn答案,我已經做了GUI更新目的另一個運行的類爲好,如下圖所示:

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

我打電話給SwingUtilities.invokeLater實際運行過程之前改變GUI .exec()來調用外部程序如下顯示:

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

但所有以上驗證碼,圖形用戶界面仍然沒有更新和程序完成後纔會更新。在協調這兩個可運行和線程的任何特定步驟中,我錯了嗎?

預先感謝您的大力幫助和回答

P/S:我開始按鈕時執行的CmdExec()在GUI下面被壓:

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

你可以在哪裏啓動CmdExec線程顯示代碼? – jtahlborn 2011-04-01 12:22:43

+0

hi jtahlborn,我在上面的mian contentn中附加了代碼,它是在GUI中按下按鈕時觸發的事件,謝謝=) – striky 2011-04-01 12:35:25

回答

0

需要調用tryDemo .start(),而不是tryDemo.run()。你直接運行線程,而不是產生一個單獨的調用。

+0

哦,我有多傻,jtahlborn你救了我一天,我真的很感激你的樣子幫助從一開始到現在,真的非常感謝你!!!!! – striky 2011-04-01 12:44:17