怎么让dos窗口的状态信息实时显示在winform界面上

zhenghui2915 2010-11-17 09:19:42
各位高手大家好,今天遇到了一个问题。
现在手上有别人编写好的可执行文件,在dos下执行该可执行文件,dos窗口给出了实时的处理状态显示,现在在c# Winform上添加一个多行文本框,希望也能够实现实时地显示处理状态信息。

现在能够实现dos处理结束后所有的状态信息一次性地写入richtext,主要是处理过程中在 Winform上的richtext中没有数据,知道处理结束,所有的状态信息一次性出现。这样会让人感觉处理过程中程序死机。所以希望能够实时把dos状态用richtext来显示,请问大家我该怎么办?
...全文
599 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly57830689 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fangxinggood 的回复:]
你的Winform里应该是通过Process启动这个Dos程序的吧。

我再写个简单的例子:
C# code……
[/Quote]

运行了一下代码,发现很严重的问题:
button2_Click后:任务管理器中的cmd.exe线程和ping.exe线程依然存在,并且ping.exe依然在继续ping。
关闭程序后:cmd.exe线程和ping.exe线程依然存在,并且ping.exe依然在继续ping。
对于process理解不多,还望解释。
huoyinghaizeizei 2010-11-23
  • 打赏
  • 举报
回复
学习~~~~~~~~~~~~~~~~~~~
zhenghui2915 2010-11-18
  • 打赏
  • 举报
回复
按照你的代码编写了,运行出错,给出的提示是:StandardError 尚未重定向。
机器人 2010-11-18
  • 打赏
  • 举报
回复
你的Winform里应该是通过Process启动这个Dos程序的吧。

我再写个简单的例子:


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 ConsoleOutputRead
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private Process _process = null;

private void button1_Click(object sender, EventArgs e)
{
//启动cmd.exe 无限输出ping localhost的结果。
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = " /c ping localhost /t";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;

_process = new Process();
_process.StartInfo = psi;
// 定义接收消息的Handler
_process.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

_process.Start();
// 开始接收
_process.BeginOutputReadLine();
}

void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
AddMessageHandler handler = delegate(string msg)
{
this.textBox1.Text += msg + Environment.NewLine;
this.textBox1.Select(this.textBox1.Text.Length-1, 0);
this.textBox1.ScrollToCaret();
};

if (this.textBox1.InvokeRequired)
this.textBox1.Invoke(handler, e.Data);
}

private delegate void AddMessageHandler(string msg);

private void button2_Click(object sender, EventArgs e)
{
_process.CancelOutputRead();
_process.Close();
}

private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.HideSelection = false;
}
}
}
zhenghui2915 2010-11-18
  • 打赏
  • 举报
回复

// 开始接收
_process.BeginOutputReadLine();
修改为:
_process.BeginOutputReadLine();
就能实现功能。

谢谢这位高手的解答。
机器人 2010-11-17
  • 打赏
  • 举报
回复
用Process的BeginOutputReadLine方法,同步输出

1. UseShellExecute = false
2. RedirectStandardOutput = true
3. 绑定 OutputDataReceived 事件
4. Process.Start
5. 调用 BeginOutputReadLine 方法


// Define the namespaces used by this sample.
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;

namespace ProcessAsyncStreamSamples
{
class SortOutputRedirection
{
// Define static variables shared by class methods.
private static StringBuilder sortOutput = null;
private static int numOutputLines = 0;

public static void SortInputListText()
{
// Initialize the process and its StartInfo properties.
// The sort command is a console application that
// reads and sorts text input.

Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";

// Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false;

// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = true;
sortOutput = new StringBuilder("");

// Set our event handler to asynchronously read the sort output.
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

// Redirect standard input as well. This stream
// is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = true;

// Start the process.
sortProcess.Start();

// Use a stream writer to synchronously write the sort input.
StreamWriter sortStreamWriter = sortProcess.StandardInput;

// Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine();

// Prompt the user for input text lines. Write each
// line to the redirected input stream of the sort command.
Console.WriteLine("Ready to sort up to 50 lines of text");

String inputText;
int numInputLines = 0;
do
{
Console.WriteLine("Enter a text line (or press the Enter key to stop):");

inputText = Console.ReadLine();
if (!String.IsNullOrEmpty(inputText))
{
numInputLines ++;
sortStreamWriter.WriteLine(inputText);
}
}
while (!String.IsNullOrEmpty(inputText) && (numInputLines < 50));
Console.WriteLine("<end of input stream>");
Console.WriteLine();

// End the input stream to the sort command.
sortStreamWriter.Close();

// Wait for the sort process to write the sorted text lines.
sortProcess.WaitForExit();

if (numOutputLines > 0)
{
// Write the formatted and sorted output to the console.
Console.WriteLine(" Sort results = {0} sorted text line(s) ",
numOutputLines);
Console.WriteLine("----------");
Console.WriteLine(sortOutput);
}
else
{
Console.WriteLine(" No input lines were sorted.");
}

sortProcess.Close();
}

private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
numOutputLines++;

// Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
}
}
}
}
zhenghui2915 2010-11-17
  • 打赏
  • 举报
回复
楼上的能不能将详细一点呀,没能看懂喔,惭愧。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧