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

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

现在能够实现dos处理结束后所有的状态信息一次性地写入richtext,主要是处理过程中在 Winform上的richtext中没有数据,知道处理结束,所有的状态信息一次性出现。这样会让人感觉处理过程中程序死机。所以希望能够实时把dos状态用richtext来显示,请问大家我该怎么办?
...全文
565 7 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
楼上的能不能将详细一点呀,没能看懂喔,惭愧。
汇编集成环境的开发 摘 要 传统的汇编程序开发主要在DOS系统下,使用EDIT、MASM、LINK、DEBUG的四步命令模式,其步骤繁琐,界面不友好,给教学和使用带来了一定的困难,因此有必要开发一个集编辑、编译、连接、调试一体化的集成开发环境,使对汇编语言的编译、连接和执行操作方便、界面友好、编程效率高。 Visual C#是一种方便快捷的编程语言,其所属的编程平台virual studio强调以组件为基础的软件开发方法。利用C#开发Windows应用程序可以在速度上获得极大的提高。另外,值得称道的是,C#所开发的软件具有高度的可移植性,能适应多种系统平台。本课题的重点在于对控制台程序的输入输出进行重定向。在程序设计中使用的是C#的winform编程模式,通过对C#控件的利用,以及对windows API函数的调用,最终完成本设计。 关键词:控制台输入输出重定向,汇编集成环境,C# The Development of Assembler Integrated Environment ABSTRACT The tradition mode of assembly program development works in DOS, uses EDIT、MASM、LINK、DEBUG four steps of DOS commands pattern mainly , whose step is miscellaneous and trivial , the interface is not amicable , brings teaching and using about certain difficulty ,therefore, it is necessary for developing a integrated exploitation environment gather editing , compiling , linking , debugging. It makes the compiling of assembly language, the operation of linking up and carrying out convenient. The interface is amicable and the efficiency of programming is high. Visual C# is a very convenient and rapid program designing language, it belongs to Visual studio platform; the program designing platform emphasizes a software developing method, which takes the module as the foundation. Developing the Windows application program with Visual C# is handy. What’s more, C# develops software, actually, which can adapt many other kinds of platforms. The key point of this project is how to redirect the input/output of the console. Windows Forms mode of C# is used in the software designing. With the grasping to C#, as well as to windows the API function, I finally complete this designing. KEY WORDS: I/O redirection of Console, Assembler integrated environment, C # 目 录 摘 要 I ABSTRACT II 1 绪 论 1 2 开发环境以及编程方法介绍 2 2.1 软件开发平台简介 2 2.1.1 .NET框架 2 2.1.2 集成开发环境 2 2.1.3 C# 简介 3 2.2 系统需求 4 2.3编程模式 5 3 MASM介绍 7 3.1 MASM的语法以及相关参数 7 3.2 LINK的语法以及相关参数 9 3.3 DEBUG的语法以及相关命令 11 3.3.1 DEBUG语法 11 3.3.2 DEBUG相关命令 12 4 软件开发过程 15 4.1 需求分析 15 4.1.1 功能定义 15 4.1.2 软件结构 15 4.1.3 可行性分析 15 4.2 详细设计 16 4.2.1 项目建立 16 4.2.2 欢迎界面 16 4.2.3 主窗口 17 4.2.4文件操作 19 4.2.5 文本处理 21 4.2.6 格式修改 22 4.2.7 汇编源文件的操作 23 4.2.8任务栏图标及帮助 26 4.3 软件测试 27 4.4 总结 30 致 谢 31 参 考 文 献 32 附 录 33
没法下载,到这里折腾一把试试。 本文由abc2253130贡献 doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。 C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 型、接口类型、委托类型和数组类型。 变量的类型声明 变量的类型声明 每个变量必须预先声明其类型。如 int a; int b = 100; float j = 4.5; string s1; 用 object 可以表示所有的类型。 预定义类型 下表列出了预定义类型,并说明如何使用。 类型 object 说明 所有其他类型的最终 基类型 字符串类型; 字符串是 Unicode 字符序列 8 位有符号整型 16 位有符号整型 32 位有符号整型 64 位有符号整型 示例 object o = null; 范围 string sbyte short int long string s = "hello"; sbyte val = 12; short val = 12; int val = 12; long val1 = 12; -128 到 127 -32,768 到 32,767 -2,147,483,648 2,147,483,647 -9,223,372,036,854,775,808 到 第1页 C#(WINFORM)学习 long val2 = 34L; 到 9,223,372,036,854,775,807 byte ushort 8 位无符号整型 16 位无符号整型 byte val1 = 12; ushort val1 = 12; uint val1 = 12; uint 32 位无符号整型 uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong 64 位无符号整型 ulong val3 = 56L; ulong val4 = 78UL; float 单精度浮点型 float val = 1.23F;7 位 double val1 = 1.23; double 双精度浮点型 double val2 = ±5.0 × 10?324 ±1.7 × 10 308 0 到 255 0 到 65,535 0 到 4,294,967,295 0 到 18,446,744,073,709,551,615 ±1.5 × 10?45 ±3.4 × 10 38 到 到 4.56D;15-16 布尔型;bool 值或为 真或为假 字符类型;char 值是 一个 Unicode 字符 精确的小数类型, 具有 28 个有效数字 bool val1 = true; bool val2 = false; char val = 'h'; decimal val = bool char decimal DateTime ±1.0 × 10?28 ±7.9 × 10 28 到 1.23M;28-29 变量转换 简单转换: float f = 100.1234f; 可以用括号转换: short s = (short)f 也可以利用 Convert 方法来转换: string s1; s1=Convert.ToString(a); MessageBox.Show(s1); 常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 Convert.ToSByte Convert.ToSingle Convert.ToString Convert.ToUInt16 Convert.ToUInt32 Convert.ToUInt64 备注 Math 类 常用科学计算方法: C# Math.Abs Math.Sqrt Math.Ro

110,502

社区成员

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

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

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