zoukankan      html  css  js  c++  java
  • 异步获取CMD命令行输出内容

    当控制台命令使用process.Start(); 后可以直接显示输出内容,当然它是异步显示的不用等程序结束。代码如下:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;

    namespace testApplication1
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }

            
    private void button1_Click(object sender, EventArgs e)
            {
                
    using (Process process = new System.Diagnostics.Process())  
                { 
                    process.StartInfo.FileName 
    = "ping"
                    process.StartInfo.Arguments 
    = "127.0.0.1 -t";
                    process.StartInfo.UseShellExecute 
    = false;
                    process.StartInfo.CreateNoWindow 
    = true;  
                    process.StartInfo.RedirectStandardOutput 
    = true
                    
                    process.Start(); 
                    process.BeginOutputReadLine();  
                    process.OutputDataReceived 
    += new DataReceivedEventHandler(process_OutputDataReceived);  
                }  
            }

            
    private void process_OutputDataReceived(object sender, DataReceivedEventArgs e) 
            {
                
    if (!string.IsNullOrEmpty(e.Data))
                    
    this.AppendText(e.Data + Environment.NewLine);   
            }
            
    public delegate void AppendTextCallback(string text);  
            
            
    public void AppendText(string text)  
            { 
                
    if (this.textBox1.InvokeRequired)  
                {  
                    AppendTextCallback d 
    = new AppendTextCallback(AppendText);  
                    
    this.textBox1.Invoke(d, text); 
                } 
                
    else 
                {  
                    
    this.textBox1.AppendText(text);                  
                } 
            }  
        }
    }

    程序是一个ping -t的示例:

  • 相关阅读:
    Use MusicBrainz in iOS(三)查询专辑的完整信息
    内存寻址一(分段)
    POJ 1018 Communication System (动态规划)
    利用Eclipse中的Maven构建Web项目(二)
    cocos2d-x2.2.3学习
    (排序算法整理)NEFU 30/32
    POJ 1066 昂贵的聘礼
    2014年腾讯暑假实习软件开发笔试题汇总
    Android学习之——自己搭建Http框架(1)
    C 语言之预处理 ---------文件包括
  • 原文地址:https://www.cnblogs.com/wangbin5542/p/1753126.html
Copyright © 2011-2022 走看看