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 BPA
{
// 1.定義委託
public delegate void DelReadStdOutput(string result);
public delegate void DelReadErrOutput(string result);
public partial class CMD : Form
{
// 2.定義委託事件
public event DelReadStdOutput ReadStdOutput;
public event DelReadErrOutput ReadErrOutput;
public CMD()
{
InitializeComponent();
Init();
}
private void Init()
{
//3.將相應函數註冊到委託事件中
ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);
ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);
}
private void RealAction(string StartFileName, string StartFileArg)
{
Process CmdProcess = new Process();
CmdProcess.StartInfo.FileName = StartFileName; // 命令
CmdProcess.StartInfo.Arguments = StartFileArg; // 參數
CmdProcess.StartInfo.CreateNoWindow = true; // 不創建新窗口
CmdProcess.StartInfo.UseShellExecute = false;
CmdProcess.StartInfo.RedirectStandardInput = true; // 重定向輸入
CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向標準輸出
CmdProcess.StartInfo.RedirectStandardError = true; // 重定向錯誤輸出
//CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
CmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
CmdProcess.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
CmdProcess.EnableRaisingEvents = true; // 啓用Exited事件
CmdProcess.Exited += new EventHandler(CmdProcess_Exited); // 註冊進程結束事件
CmdProcess.Start();
CmdProcess.StandardInput.WriteLine(StartFileArg);
CmdProcess.BeginOutputReadLine();
CmdProcess.BeginErrorReadLine();
//CmdProcess.
// MessageBox.Show(CmdProcess.StandardOutput.ReadToEnd());
// 如果打開註釋,則以同步方式執行命令,此例子中用Exited事件異步執行。
// CmdProcess.WaitForExit();
}
private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
// 4. 異步調用,需要invoke
this.Invoke(ReadStdOutput, new object[] { e.Data });
}
}
private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
this.Invoke(ReadErrOutput, new object[] { e.Data });
}
}
private void ReadStdOutputAction(string result)
{
// MessageBox.Show(result + "\r\n");
this.textBoxShowStdRet.AppendText(result + "\r\n");
}
private void ReadErrOutputAction(string result)
{
//MessageBox.Show(result + "\r\n");
this.textBoxShowStdRet.AppendText(result + "\r\n");
}
private void CmdProcess_Exited(object sender, EventArgs e)
{
// 執行結束後觸發
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// 啓動進程執行相應命令,此例中以執行ping.exe爲例
RealAction("cmd.exe", textBox1.Text);
}
}
}
c# 異步調用dos命令
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
DALFactory有什麼作用
Rshh
2018-08-27 20:02:23
C#跨線程調用窗體控件的問題
Rshh
2018-08-27 20:02:23
C#讀取excel--OLEDB方式
kingbaron
2018-08-27 19:59:47
ASP.NET實用分頁代碼
卤蛋哥
2018-08-27 19:50:29
C#之接口(Interface)
卤蛋哥
2018-08-27 19:50:27
〔C# Note〕面向對象篇
lkisgeek
2018-08-27 19:42:40
RGB,CMYK,HSB各種顏色表示的轉換
Timini
2018-08-27 19:38:33
Linq一些方法整理
侯鸡仔
2018-08-27 19:30:35
ftp圖片上傳下載帶進度條
wellbeingzhang
2018-08-27 19:29:06
GMap.Net根據地址搜索,並雙擊獲得經緯度((Winform)最新版本的代碼)
wellbeingzhang
2018-08-27 19:29:06
VisualStudio App.Config自動配置
北京的雾霾天
2018-08-27 19:27:34
mvc中將視圖輸出成字符串
北京的雾霾天
2018-08-27 19:27:33