2011-12-15 30 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。

相关问题