代码改变世界

[转载]两个Form,一个Form中处理数据,另一个是ShowDialog()出来的上面就一个progressBar用来显示进度!!

2007-08-13 17:29  Virus-BeautyCode  阅读(2468)  评论(0编辑  收藏  举报
//------------------------------------------------------------------------------
/// <copyright from='1997' to='2001' company='Microsoft Corporation'>
/// 版权所有 (c) Microsoft Corporation。保留所有权利。
///
/// 此源代码仅作为 Microsoft 开发工具和/或联机文档
/// 的补充。有关 Microsoft 代码示例的详细信息,请
/// 参阅这些其他资料。

///
/// </copyright>
//------------------------------------------------------------------------------
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());
}


}
}