奇怪的问题,.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’。问题在哪里?把进度条的操作关掉就会正常。
...全文
126 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
efoxy 2004-01-02
  • 打赏
  • 举报
回复
o~~~~~~~~~~~~~~~~~~~~~~~~IC,实在不好意思,明白了,实际弹出的结果是另一个线程已经改变后的z值。呵呵,低级问题。马上结贴加分。
IamBM 2004-01-01
  • 打赏
  • 举报
回复
int i,j;
for(i...){
for(j...){
...
}
int k=i+j;
MessageBox.Show(k.ToString());
IamBM 2004-01-01
  • 打赏
  • 举报
回复
多个线程同时对一个变量修改,当然不正常
1979xt 2004-01-01
  • 打赏
  • 举报
回复
再补充一句,你的i和j虽然是在每个线程里不一样,但z是线程共享的
1979xt 2004-01-01
  • 打赏
  • 举报
回复
线程是共享变量的,用lock关键字就可以锁定这个变量被一个线程访问时,别的线程就不能访问,不过只能锁定引用类型,值类型不行.

efoxy 2004-01-01
  • 打赏
  • 举报
回复
up
DaemonFTPer 2004-01-01
  • 打赏
  • 举报
回复
同步对象.
dldl 2004-01-01
  • 打赏
  • 举报
回复
同意 IamBM
inethax 2004-01-01
  • 打赏
  • 举报
回复
up
efoxy 2004-01-01
  • 打赏
  • 举报
回复
每个线程不是维护自己单独的可见域吗??如果是的话就应该两个线程之间的变量没有影响,虽然进度条的是两个线程共同操作的对象,最后显示的结果和进度条无关啊,为什么操作进度条就会出错,而不操作就会结果正确呢??而且每次执行得到的错误结果还不一样。我真的迷茫了,哪位给个具体点的的解释。





俺就想讨个说法~~~~~
-_-中英文数字 2004-01-01
  • 打赏
  • 举报
回复
同意 IamBM(商用机器)
efoxy 2003-12-31
  • 打赏
  • 举报
回复
z的最大值是1996,绝对没有超出范围,进度条的最大值也是1996也没有问题,感兴趣的可以编译一下
graying 2003-12-31
  • 打赏
  • 举报
回复
设置进度条的value前记得把它的最大值设置的比z大。
MichaelSoft 2003-12-31
  • 打赏
  • 举报
回复
值超出范围了

110,536

社区成员

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

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

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