与外部程序输出刷新表单控件 [英] Refresh form controls with external program output

查看:118
本文介绍了与外部程序输出刷新表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的WinForm的,调用外部程序(SVN)。我想显示由SVN在形式的文本输出的结果。

I have a basic winform, calling an external program (SVN). I want to display the output produced by SVN in a textbox in the form.

下面是我的code调用这个外部程序:

Here is my code for calling this external program :

private void SVNcmd(string args, string workingcopy)
{
    textBoxOutput.Text += workingcopy + Environment.NewLine
        + args + Environment.NewLine;
    Process p = new Process();
    p.StartInfo.WorkingDirectory = workingcopy;
    p.StartInfo.FileName = "svn";
    p.StartInfo.Arguments = args;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.Start();
    textBoxOutput.Text += p.StandardOutput.ReadToEnd() + Environment.NewLine;
    p.WaitForExit(); 
}

这就是所谓的内部的foreach。问题是,当我启动命令,我必须等待,直到每一个命令已经完成......这可能需要相当长的时间。在此期间,形式冻结,并没有显示在文本框中。
也许这种方法的命令的同时启动,尽管WaitForExit?我不知道,我不熟悉这种类型的问题(我是一个Web开发人员的)。

It's called inside a "foreach". The problem is, when I launch the command, I have to wait until each and every command has been finished... and it can take quite a while. During this time, the form freezes, and nothing is displayed in the textbox.
Maybe with this method the commands are launched at the same time, despite the WaitForExit ? I'm not sure, I'm not familiar with this type of problems (I'm more of a web developer).

我能做些什么,以显示SVN输出和冻结停止形式在程序运行时?

What can I do to display the SVN output and stop the form from freezing while the program runs ?

感谢

推荐答案

使用一个BackgroundWorker来做到这一点。在backgroundworder可以报告进展并输出到用户界面。

Use a backgroundworker to do this. In the backgroundworder you can report progress and output to the UI.

很好的例子在 MSDN

让你的工人DoWork的做你的code。当你得到读的线条,你应该做的worker.ReportProgress。注册该事件,并从参数(事件)就行了。

Let you worker DoWork do your code. Where you get read the lines you should do worker.ReportProgress. Register on that event and get the line from the param (the event).

这篇关于与外部程序输出刷新表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆