当前位置:网站首页>C process runs the asynchronous echo of CMD command
C process runs the asynchronous echo of CMD command
2022-04-22 19:58:00 【Baby Cabbage!!】
The following code is new Process() call cmd command , And asynchronously echo the result to Form Example :
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Xml;
namespace CmdCallbackShow
{
// 1. Define the entrusted
public delegate void DelReadStdOutput(string result);
public delegate void DelReadErrOutput(string result);
public partial class MainWindow : Window
{
// 2. Define delegate events
public event DelReadStdOutput ReadStdOutput;
public event DelReadErrOutput ReadErrOutput;
public Form1()
{
InitializeComponent();
Init();
}
private void Init()
{
//3. Register the corresponding function in the delegate event
ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);
ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);
}
private void button1_Click(object sender, EventArgs e)
{
// Start the process and execute the corresponding command , In this example, execute ping.exe For example
RealAction("ping.exe", textBox1.Text);
}
public Process CmdProcess=null;
private void RealAction(string StartFileName, string StartFileArg)
{
CmdProcess = new Process();
CmdProcess.StartInfo.FileName = StartFileName; // command
CmdProcess.StartInfo.Arguments = StartFileArg; // Parameters
CmdProcess.StartInfo.CreateNoWindow = true; // Do not create a new window
CmdProcess.StartInfo.UseShellExecute = false;
CmdProcess.StartInfo.RedirectStandardInput = true; // Redirect input
CmdProcess.StartInfo.RedirectStandardOutput = true; // Redirect standard output
CmdProcess.StartInfo.RedirectStandardError = true; // Redirect error output
//CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
CmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
CmdProcess.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
CmdProcess.EnableRaisingEvents = true; // Enable Exited event
CmdProcess.Exited += new EventHandler(CmdProcess_Exited); // Registration process end event
CmdProcess.Start();
CmdProcess.BeginOutputReadLine();
CmdProcess.BeginErrorReadLine();
// If you open the comment , Execute the command in a synchronous manner , Used in this example Exited Events execute asynchronously .
// CmdProcess.WaitForExit();
}
// 4. Asynchronous call , need invoke
private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
// Switch between different threads Context operations
Dispatcher.Invoke(ReadStdOutput, new object[] { e.Data }); // This is a wpf Need to use dispatcher, If it is winform The project is this.
}
}
private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
Dispatcher.Invoke(ReadErrOutput, new object[] { e.Data });
}
}
private void ReadStdOutputAction(string result)
{
this.textBoxShowStdRet.AppendText(result + "\r\n");
}
private void ReadErrOutputAction(string result)
{
this.textBoxShowErrRet.AppendText(result + "\r\n");
}
private void CmdProcess_Exited(object sender, EventArgs e)
{
if (CmdProcess != null)
CmdProcess.Close();
}
}
reference : turn C# Process function cmd Asynchronous echo of command - ssboy - Blog Garden (cnblogs.com)
版权声明
本文为[Baby Cabbage!!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221951443102.html
边栏推荐
- [Architecture] Why do you need master-slave replication and fragmentation
- When MySQL designs a table, two timestamp fields are required
- Industry trends are far more important than efforts -- top test technology summary
- Format for creating Zimbra LDAP users using LDAP clients
- Packaging PHP with rpmbuild
- 面试千万不要这么说,“不喜欢开发,所以选择测试”
- From functional test to automatic test, the salary doubled. I sorted out this 3000 word super complete learning guide
- Pytorch deep learning practice 09 multi classification questions
- Redis 最全解读
- 互联网快讯:联想公布ESG新进展;极米H3S、极米Z6X Pro出色音画获好评;小红书回应“裁员20%”
猜你喜欢

More than 100 days, 0 basic self-study and career change software testing, from 3000 to 15K monthly salary, I compiled a super complete learning guide
![[2022 fresh students see] can an inexperienced college graduate change his career to do software testing?](/img/62/3726b18cd949c30a717845b519d22c.png)
[2022 fresh students see] can an inexperienced college graduate change his career to do software testing?

从功能测试到自动化测试,待遇翻倍,我整理了这一份3000字超全学习指南

DNS resource records detailed explanation & authority | recursive resolution difference (super detailed)

图像预训练模型的起源解说和使用示例

顶测科技整理转行过程中一定要避免的问题

Jmeter:一文2000字接口测试基础知识介绍

互联网快讯:联想公布ESG新进展;极米H3S、极米Z6X Pro出色音画获好评;小红书回应“裁员20%”
![[eight part essay] usage scenarios and features of JUC](/img/7d/3ccbea7652b52e6e62581fc612fe46.png)
[eight part essay] usage scenarios and features of JUC

网络隧道技术
随机推荐
【八股文】线程安全问题
Jmeter:一文2000字接口测试基础知识介绍
Software testing industry must see, a text of 800 words to teach you how to build the allure test report environment
DNS resource records detailed explanation & authority | recursive resolution difference (super detailed)
CVPR 2022: is smile recognition also sexist? Zhejiang University and Wuhan University jointly set up a fairness improvement framework
骗子用AI语音获利近1.8亿,受害者:听不出来是机器人啊
Open source, free and best used 3 systems and 9 firewall software Amway gives you
DNS解析流程&基础知识
应届毕业生的头一份工作有多重要?
【牛客刷题19】MP3光标位置
x16为什么比x8慢?
5篇关于强化学习在金融领域中应用的论文推荐
Deux mécanismes de désactivation de la vérification des messages courts et leurs différences
短信验证两种定时禁用处理机制及区别
时序数据库市场漫谈
【Unity】可玩广告Luna Playable插件的踩坑记录
传:阿里达摩院裁员30%! 内部员工:谣言! 网友:要不裁29.99%。。。
go语言的时间格式
【Unity/C#】游戏出现区域性崩溃,深藏的国际化巨坑
DNS resolution process & Basics