内容如下:
Form1 中单击 开始 按钮----弹出进度条窗体Form2----等Form3 的数据加载完成后---关闭Form2,显示Form3
代码如下:
Form1using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 回调
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(144, 48);
this.button1.TabIndex = 0;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 157);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Class1.Send("001","ppp",null);
} private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
-------------------------------------------------
Form2:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace 回调
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.ProgressBar progressBar1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
Class1.EventDelegate+=new Class1.EventDelegateHandler(ReturnMsg); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(8, 8);
this.progressBar1.Maximum = 70000;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(336, 16);
this.progressBar1.TabIndex = 2;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 29);
this.ControlBox = false;
this.Controls.Add(this.progressBar1);
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "进度条";
this.Load += new System.EventHandler(this.Form2_Load);
this.Activated += new System.EventHandler(this.Form2_Activated);
this.ResumeLayout(false); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
} private void ReturnMsg(string MsgName,string MsgId,System.Xml.XmlNode ReturnData)
{
//this.label1.Text=MsgName;
}
private void button1_Click(object sender, System.EventArgs e)
{
//Class1.Send("Sdf","ppp",null);
} private void MyAdd()
{
for (int i = 0; i < 1000 ; i++)
{
double m = 0;
//System.Threading.Thread.Sleep(1);
this.progressBar1.Value = i;
//this.progressBar1.Refresh();
}
} private void Form2_Activated(object sender, System.EventArgs e)
{
Thread thread = new Thread(new ThreadStart(MyAdd));
thread.Start();
//this.MyAdd();
//ThreadFun();
}
private void ThreadFun()
{ //Create invoke method by specific function
MethodInvoker mi = new MethodInvoker( this.InvokeFun );
for( int i = 0; i < 100; i++ )
{ this.BeginInvoke( mi );
Thread.Sleep( 100 );
}
}
private void InvokeFun()
{ if( progressBar1.Value < 1000 ) progressBar1.Value = progressBar1.Value + 1; }
}
}
Form1 中单击 开始 按钮----弹出进度条窗体Form2----等Form3 的数据加载完成后---关闭Form2,显示Form3
代码如下:
Form1using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 回调
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(144, 48);
this.button1.TabIndex = 0;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 157);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Class1.Send("001","ppp",null);
} private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
-------------------------------------------------
Form2:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace 回调
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.ProgressBar progressBar1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
Class1.EventDelegate+=new Class1.EventDelegateHandler(ReturnMsg); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(8, 8);
this.progressBar1.Maximum = 70000;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(336, 16);
this.progressBar1.TabIndex = 2;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 29);
this.ControlBox = false;
this.Controls.Add(this.progressBar1);
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "进度条";
this.Load += new System.EventHandler(this.Form2_Load);
this.Activated += new System.EventHandler(this.Form2_Activated);
this.ResumeLayout(false); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
} private void ReturnMsg(string MsgName,string MsgId,System.Xml.XmlNode ReturnData)
{
//this.label1.Text=MsgName;
}
private void button1_Click(object sender, System.EventArgs e)
{
//Class1.Send("Sdf","ppp",null);
} private void MyAdd()
{
for (int i = 0; i < 1000 ; i++)
{
double m = 0;
//System.Threading.Thread.Sleep(1);
this.progressBar1.Value = i;
//this.progressBar1.Refresh();
}
} private void Form2_Activated(object sender, System.EventArgs e)
{
Thread thread = new Thread(new ThreadStart(MyAdd));
thread.Start();
//this.MyAdd();
//ThreadFun();
}
private void ThreadFun()
{ //Create invoke method by specific function
MethodInvoker mi = new MethodInvoker( this.InvokeFun );
for( int i = 0; i < 100; i++ )
{ this.BeginInvoke( mi );
Thread.Sleep( 100 );
}
}
private void InvokeFun()
{ if( progressBar1.Value < 1000 ) progressBar1.Value = progressBar1.Value + 1; }
}
}
-------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace 回调
{
/// <summary>
/// Form3 的摘要说明。
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form3()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
Class1.EventDelegate+=new Class1.EventDelegateHandler(ReturnMsg);
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 120);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(288, 293);
this.Controls.Add(this.label1);
this.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
this.ResumeLayout(false); }
#endregion private void Form3_Load(object sender, System.EventArgs e)
{
}
private void ReturnMsg(string MsgName,string MsgId,System.Xml.XmlNode ReturnData)
{
this.label1.Text=MsgName;
int p=0; if (MsgName=="001")
{
//开始加载数据
for (int i = 0; i < 10000 ; i++)
{
double m = 0;
for (int k = 0; k < 10000 ; ++k)
{
m += k;
}
//this.progressBar1.Value = i;
} Class1.Send("002","结束",null);
}
} }
}
---------------
class1:using System;
using System.Xml;namespace 回调
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
public delegate void EventDelegateHandler(string MsgName,string MsgID,XmlNode ReturnData);
public static EventDelegateHandler EventDelegate;
public static Form2 obj=null;
public static Form3 obj1=null;
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void Send(string MsgName,string MsgID,XmlNode ReturnData)
{
if (MsgName=="001")
{
obj1=new Form3();
obj=new Form2();
obj.Show();
}
if (MsgName=="002")
{
obj.Close();
obj1.Show();
} if (EventDelegate!=null)
EventDelegate(MsgName,MsgID,ReturnData);
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication37
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(288, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 0;
this.button1.Text = "操作";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 93);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "多线程显示进度条";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
}
}Form2的using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication37
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label label1;
private Thread thread; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
thread = new Thread(new ThreadStart(MySub));
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(40, 40);
this.progressBar1.Maximum = 5000;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(280, 16);
this.progressBar1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(128, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 24);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(378, 111);
this.ControlBox = false;
this.Controls.Add(this.label1);
this.Controls.Add(this.progressBar1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "请稍候........";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
this.thread.Start();
//MySub();
} private void MySub()
{
double x = 0;
for (int i=1;i<=100;i++)
{
for (int j=1;j<=50;j++)
{
x++;
//this.Text = x.ToString();
label1.Text= x.ToString()+"/5000";
progressBar1.Value=(int)x;
if((int)x==5000)
{
this.Close();
}
}
} } }
}