2011-12-15 28 views
1

在我的應用程序中,我調用了一個外部命令行工具來將iso與其他文件格式進行交互。現在我只是調用iso轉換器,它會在後臺運行,當你通過命令行運行iso轉換器時,你會看到它在做什麼,但是在我的應用程序中它只是在後臺運行。VB.NET從其他應用程序獲取實時狀態

現在它只是讓我的狀態isoconverter在一個文本框完成後,我怎麼能改變這個,所以我可以看直播狀態?就像我會在命令行工具中看到的一樣?

這是我打電話來執行isoconverter代碼。

Private Sub GETCMD3() 
    Dim CMDprocess As New Process 
    Dim StartInfo As New System.Diagnostics.ProcessStartInfo 
    StartInfo.FileName = "cmd" 
    StartInfo.CreateNoWindow = True 
    StartInfo.RedirectStandardInput = True 
    StartInfo.RedirectStandardOutput = True 
    StartInfo.UseShellExecute = False 
    CMDprocess.StartInfo = StartInfo 
    CMDprocess.Start() 
    Dim SR As System.IO.StreamReader = CMDprocess.StandardOutput 
    Dim SW As System.IO.StreamWriter = CMDprocess.StandardInput 
    SW.WriteLine("Isoconvert.exe " & txtIsoFile.Text) 
    SW.WriteLine("exit") 
    txtIsoOutput.Text = SR.ReadToEnd 
    SW.Close() 
    SR.Close() 
End Sub 

回答

2

與您現有的代碼的問題是該行

txtIsoOutput.Text = SR.ReadToEnd 

這是讀取命令的標準輸出流,直到它完成。一旦完成,它會將結果分配給您的文本框。

想要改爲使用StreamReader.ReadLineReadBlockStreamReader中稍微閱讀一次。

喜歡的東西:

Dim line as String 
Do 
    line = SR.ReadLine() 
    If Not (line Is Nothing) Then 
     txtIsoOutput.Text = txtIsoOutput.Text + line + Environment.NewLine 
    End If 
Loop Until line Is Nothing 

這可能是不夠好,雖然。用戶界面線程現在忙於處理命令輸出,所以TextBox沒有機會更新其顯示。解決此問題的最簡單方法是在修改文本後添加Application.DoEvents()。不過,請確保在啓動GETCMD3時禁用所有調用GETCMD3的按鈕/菜單。

0

我不確定,也許訪問進程線程和檢查狀態?

事情是這樣的:

CMDprocess.Threads(0).ThreadState = ThreadState.Running 
+0

我怎樣才能使用這段代碼?對不起,我是新來的VB – PandaNL 2011-12-15 12:18:50

+0

找到這個鏈接http://support.microsoft.com/kb/173085/但我不知道如何在我的代碼中實現它。 – PandaNL 2011-12-15 12:47:37

1

[Offtopic]我正在審查你的代碼,也許我發現你可以啓動Isoconvert.exe的方式更好的形式給出。

如果我沒看錯,你可以使用的StartInfo,而不需要啓動的控制檯命令啓動Isoconvert.exe。

Dim CMDprocess As New Process 
Dim StartInfo As New System.Diagnostics.ProcessStartInfo 
StartInfo.FileName = "Isoconvert.exe" 
StartInfo.Arguments = txtIsoFile.Text 
CMDprocess.StartInfo = StartInfo 
CMDprocess.Start() 

我認爲你仍然可以讀寫stdin和stdout。

相關問題