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.Threading;
namespace
WindowsFormsApplication3
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
public
delegate
void
UpdateTxt(
string
msg);
public
UpdateTxt updateTxt;
public
void
UpdateTxtMethod(
string
msg)
{
richTextBox1.AppendText(msg +
"\r\n"
);
richTextBox1.ScrollToCaret();
}
public
void
ThreadMethodTxt(
int
n)
{
this
.BeginInvoke(updateTxt,
"线程开始执行,执行"
+ n +
"次,每一秒执行一次"
);
for
(
int
i = 0; i < n; i++)
{
this
.BeginInvoke(updateTxt, i.ToString());
Thread.Sleep(1000);
}
this
.BeginInvoke(updateTxt,
"线程结束"
);
}
private
void
button1_Click(
object
sender, EventArgs e)
{
Thread objThread =
new
Thread(
new
ThreadStart(
delegate
{
ThreadMethodTxt(Convert.ToInt32(textBox1.Text.Trim()));
}));
objThread.Start();
}
private
void
Form1_Load_1(
object
sender, EventArgs e)
{
updateTxt =
new
UpdateTxt(UpdateTxtMethod);
}
}
}