大家来帮忙看一下多线程,进度条的问题,进度条显示不正常
内容如下:
Form1 中单击 开始 按钮----弹出进度条窗体Form2----等Form3 的数据加载完成后---关闭Form2,显示Form3
代码如下:
Form1
using 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;
}
}
}