首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

使用Process执行DOS命令异步输出不及时,该如何解决

2012-05-21 
使用Process执行DOS命令异步输出不及时以下代码是使用Process执行DOS命令异步输出,cmdProcess_OutputDataR

使用Process执行DOS命令异步输出不及时
以下代码是使用Process执行DOS命令异步输出,cmdProcess_OutputDataReceived中能够获取DOS信息,一行一行的获取,但是,需要在DOS命令执行完成后,cmdProcess_OutputDataReceived才会调用,现在就是需要在界面上及时显示DOS的执行情况和反馈信息,能够一有输出就获取,并在界面上显示。望大侠指教,先谢过~~~!!!

C# code
        public void ExecuteDOS(string command, int seconds)        {            //string output = ""; // 输出字符串            if (command!=null&&!command.Equals(""))            {                Process process = new Process(); //创建进程对象                ProcessStartInfo startInfo = new ProcessStartInfo();                startInfo.FileName = "cmd.exe";                startInfo.Arguments = "/C "+command;                //startInfo.Arguments = "/x " + command;                startInfo.UseShellExecute = false;  // 不使用系统外壳true,false                startInfo.RedirectStandardInput = false;    //不重定向输入false                startInfo.RedirectStandardOutput = true;  // false true//重定向输出                startInfo.CreateNoWindow = true;//false;true            //不创建窗口                process.StartInfo = startInfo;                try                {                    /*                     * 同步掉用                    if (process.Start())    // 开始进程                    {                        if (seconds == 0)                        {                            process.WaitForExit();  //这里无限等待进程结束                        }                        else                        {                            process.WaitForExit(seconds);   //等待进程结束,等待时间为指定的毫秒                        }                        output = process.StandardOutput.ReadToEnd();    //读取进程的输出                        InsertMessage(output);                    }                                         */                    /*                     * 异步调用                     */                    process.OutputDataReceived += new DataReceivedEventHandler(cmdProcess_OutputDataReceived);                    process.Start();                    process.BeginOutputReadLine();                    //Application.DoEvents();                }                catch (System.Exception ex)                {                }                finally                {                }            }        }


[解决办法]
cmdProcess_OutputDataReceived是怎么写的?这个异步的接收是一行行处理的,如果收到一行数据,立刻会触发,不会等DOS命令执行完成后才触发,倒是你的用户界面是否被锁住了,等DOS执行后才被刷新界面的显示了吧。
[解决办法]
你可以用ping指令测试,那个每隔一秒输出一行内容,默认的情况下,持续4秒。
[解决办法]
这个貌似是这样的,会多次被调用,基本是一行一次,

有时候你还会发现输出不全,呵呵

热点排行