c# 后台运行 前台实时显示

来源:百度知道 编辑:UC知道 时间:2024/09/22 22:31:56
谁能用线程帮改下,让程序边运行,边显示. 而不是全运行完再显示.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication24
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{

给你个最简单的方法,这个方法没有用到委托只是简单的使用线程,建议你看看下面这篇文章
http://www.msproject.cn/people/answer/Article/94.html
把FormClosingEventArgs e改为System.Windows.Forms.FormClosingEventArgs e就行了
这个事件就是为了关闭thread线程
System.Threading.Thread thread;
private void button1_Click(object sender, System.EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
thread = new System.Threading.Thread(new System.Threading.ThreadStart(Add));
thread.Start();
}
private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgse)
{
thread.Abort();
}
void Add()
{
for (int b = 1; b <= 200000; b++)
{
textBox1.Text += b + "\n";
}
}

这个是要用分布式吧,可惜自己学的也不行,帮不上