奇怪的问题,.net竟然999+999可以得出一个奇怪的数字,我的程序有问题吗?高手指点。。。
efoxy 2003-12-31 06:19:43 using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ThreadTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Threading.Thread tr;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ProgressBar progressBar1;
int z=0;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(112, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.progressBar1.Location = new System.Drawing.Point(16, 216);
this.progressBar1.Maximum = 1996;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(264, 23);
this.progressBar1.TabIndex = 2;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public void begin()
{
for(int i=0;i<999;i++)
for(int j=0;j<999;j++)
{
z=i+j;
this.progressBar1.Value=z;//当这行注释掉的时候结果正常。
}
MessageBox.Show(z.ToString());
}
private void button1_Click(object sender, System.EventArgs e)
{
this.tr=new System.Threading.Thread(new System.Threading.ThreadStart(begin));
tr.Start();
}
}
}
运行程序,连续点击两次button1,过一会儿会弹出两个结果对话框,两个结果应该一样才对,但先弹出的那个肯定会是一个错误的结果,第二个是正确的‘1996’。问题在哪里?把进度条的操作关掉就会正常。