是不推荐的,至于如何用BeginInvoke等方法,我可以给出一个例子看看:
namespace Microsoft.Samples.WinForms.Cs.ThreadMarshal {
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
public class ThreadMarshal : System.Windows.Forms.Form {
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar1; private Thread timerThread; public ThreadMarshal() { // Windows 窗体设计器所必需的
InitializeComponent(); } //在背景线程上执行此函数 - 它封送处理调用以
//将用户界面更新回前景线程
public void ThreadProc() { try {
MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
while (true) {
//在窗体上调用 BeginInvoke
this.BeginInvoke(mi);
Thread.Sleep(500) ;
}
}
//当该线程被主线程中断时引发 - 退出该循环
catch (ThreadInterruptedException) {
//只需退出....
}
catch (Exception) {
}
} //此函数是从背景线程调用的
private void UpdateProgress() { //如果需要,重置启动
if (progressBar1.Value == progressBar1.Maximum) {
progressBar1.Value = progressBar1.Minimum ;
} progressBar1.PerformStep() ;
} //启动背景线程以更新进度栏
private void button1_Click(object sender, System.EventArgs e) {
StopThread();
timerThread = new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground = true;
timerThread.Start();
} //停止背景线程以更新进度栏
private void button2_Click(object sender, System.EventArgs e) {
StopThread();
} //停止背景线程
private void StopThread()
{
if (timerThread != null)
{
timerThread.Interrupt();
timerThread = null;
}
} protected override void Dispose(bool disposing)
{
StopThread();
if (disposing) {
if (components != null) {
components.Dispose();
}
} base.Dispose(disposing);
} private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.button1.Location = new System.Drawing.Point(164, 74);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(153, 46);
this.button1.TabIndex = 1;
this.button1.Text = "开始!";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.button2.Location = new System.Drawing.Point(328, 74);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(153, 46);
this.button2.TabIndex = 1;
this.button2.Text = "停止!";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// progressBar1
//
this.progressBar1.Font = new System.Drawing.Font("宋体", 8F, System.Drawing.FontStyle.Bold);
this.progressBar1.Location = new System.Drawing.Point(13, 12);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(448, 46);
this.progressBar1.TabIndex = 2;
this.progressBar1.Text = "开始!";
//
// ThreadMarshal
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(501, 135);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.button2,
this.progressBar1});
this.Name = "ThreadMarshal";
this.Text = "已使用设计器生成";
this.ResumeLayout(false); } [STAThread]
public static void Main(string[] args) {
Application.Run(new ThreadMarshal());
}
}
}
namespace Microsoft.Samples.WinForms.Cs.ThreadMarshal {
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
public class ThreadMarshal : System.Windows.Forms.Form {
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar1; private Thread timerThread; public ThreadMarshal() { // Windows 窗体设计器所必需的
InitializeComponent(); } //在背景线程上执行此函数 - 它封送处理调用以
//将用户界面更新回前景线程
public void ThreadProc() { try {
MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
while (true) {
//在窗体上调用 BeginInvoke
this.BeginInvoke(mi);
Thread.Sleep(500) ;
}
}
//当该线程被主线程中断时引发 - 退出该循环
catch (ThreadInterruptedException) {
//只需退出....
}
catch (Exception) {
}
} //此函数是从背景线程调用的
private void UpdateProgress() { //如果需要,重置启动
if (progressBar1.Value == progressBar1.Maximum) {
progressBar1.Value = progressBar1.Minimum ;
} progressBar1.PerformStep() ;
} //启动背景线程以更新进度栏
private void button1_Click(object sender, System.EventArgs e) {
StopThread();
timerThread = new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground = true;
timerThread.Start();
} //停止背景线程以更新进度栏
private void button2_Click(object sender, System.EventArgs e) {
StopThread();
} //停止背景线程
private void StopThread()
{
if (timerThread != null)
{
timerThread.Interrupt();
timerThread = null;
}
} protected override void Dispose(bool disposing)
{
StopThread();
if (disposing) {
if (components != null) {
components.Dispose();
}
} base.Dispose(disposing);
} private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.button1.Location = new System.Drawing.Point(164, 74);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(153, 46);
this.button1.TabIndex = 1;
this.button1.Text = "开始!";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.button2.Location = new System.Drawing.Point(328, 74);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(153, 46);
this.button2.TabIndex = 1;
this.button2.Text = "停止!";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// progressBar1
//
this.progressBar1.Font = new System.Drawing.Font("宋体", 8F, System.Drawing.FontStyle.Bold);
this.progressBar1.Location = new System.Drawing.Point(13, 12);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(448, 46);
this.progressBar1.TabIndex = 2;
this.progressBar1.Text = "开始!";
//
// ThreadMarshal
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(501, 135);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.button2,
this.progressBar1});
this.Name = "ThreadMarshal";
this.Text = "已使用设计器生成";
this.ResumeLayout(false); } [STAThread]
public static void Main(string[] args) {
Application.Run(new ThreadMarshal());
}
}
}
在线程调用About方法后,线程并不是立刻终止,要等线程的所有finally快中的代码完成后才会完全终止. 所以在主线程中可以用Join方法来同步,当线程还未完全终止时,t.Join()将处于等待,直到t线程完全结束后再继续执行后面的语句.