大家来帮忙看一下多线程,进度条的问题,进度条显示不正常

zhoudesheng 2006-07-05 04:50:16
内容如下:
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;

}


}
}
...全文
435 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanglg 2006-07-07
  • 打赏
  • 举报
回复
关注
zhoudesheng 2006-07-06
  • 打赏
  • 举报
回复
知道一些原因了,好像是多线程的问题,首先进度条的线程能走一个值,但是接下来全部走form3 的处理代码,根本不会走进度条的线程,所以进度条看起来不动,请问如何解决?
zhoudesheng 2006-07-06
  • 打赏
  • 举报
回复
是不是回掉的代码有问题,如果我在form3 中不处理消息的话,是显示正常的
zhoudesheng 2006-07-05
  • 打赏
  • 举报
回复
哦,我是想把form2 与form3 分开
liaodanz 2006-07-05
  • 打赏
  • 举报
回复
具体处理数据就你在Form2里面进行啊..添加些代码在里面进行处理..
zhoudesheng 2006-07-05
  • 打赏
  • 举报
回复
还有Form3 吗 具体处理数据的
liaodanz 2006-07-05
  • 打赏
  • 举报
回复
我把我的贴给你吧.你复制起看看..

多线程显示进度条

Form1的

using System;
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();
}
}
}

}

}
}




liaodanz 2006-07-05
  • 打赏
  • 举报
回复
楼主..信 誉 值: 94 记到结贴哟..
zhoudesheng 2006-07-05
  • 打赏
  • 举报
回复
form3
-------------------------------------------------------
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);


}
}
}


111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧