下面是程序,执行到那一行就出错,我想是不是和多线程有关系。是不是一个竞态条件的问题?请高手指点一下!private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread a = new System.Threading.Thread(new System.Threading.ThreadStart(Plus));
a.Start();
}private void Plus()
{
this.progressBar1.Value = 0;
for (int i = 0; i < 1000; i++)
{
this.progressBar1.Value++; //一执行到这里就出错
System.Threading.Thread.Sleep(100);
if (this.progressBar1.Value == this.progressBar1.Maximum)
{
MessageBox.Show("OK");
return;
}
}
}
{
System.Threading.Thread a = new System.Threading.Thread(new System.Threading.ThreadStart(Plus));
a.Start();
}private void Plus()
{
this.progressBar1.Value = 0;
for (int i = 0; i < 1000; i++)
{
this.progressBar1.Value++; //一执行到这里就出错
System.Threading.Thread.Sleep(100);
if (this.progressBar1.Value == this.progressBar1.Maximum)
{
MessageBox.Show("OK");
return;
}
}
}
解决方案 »
- 各位大侠帮帮忙
- 页面刷新问题 FRO1产生FROM2 FROM2操作完成后 ,刷新FROM1 怎么写
- 高手请进……c#之中关于datetime结构的疑惑……
- 高手帮个忙啊。sql2000安装后 安装目录下没有MSSQL.1文件夹(在线等。。。。)
- PowerPoint二次开发-无法关闭进程
- 两个int相除求百分比,应该转换成double还是decimal?
- c#调用DLL文件发生错误!!请高手指点,急~~
- 关于多个Site使用同一个MasterPage的问题
- 郁闷!散分!
- WPF LISTVIEW 列头如何才能不显示?
- 如 何 判 断 字 符 串 里 含 有 unicode 字 符
- 当服务器端软件版本更新,客户端自动查找新版本并更新
在线程中不能直接访问空间用委托,网络上有这样的例子.
基本是路
public delege void CallSetText(string txt);
pub void SetText(string txt)
{
if (this.text1.isrequired)
{
this.text1.text=txt;
}
else
{
this.invken{
}
}
}在你的线程里直接用SetText("232");
或者Form.CheckForIllegalCrossThreadCalls = false;
和我的这个程序没有关系吧?
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread a = new System.Threading.Thread(new System.Threading.ThreadStart(Plus));
a.Start();
}
private delegate void temp();
private void Plus()
{
if(this.InvokeRequired )
this.Invoke (new temp (Plus))//这里,跨线程
else
{
this.progressBar1.Value = 0;
for (int i = 0; i < 1000; i++)
{
this.progressBar1.Value++; //
System.Threading.Thread.Sleep(100);
if (this.progressBar1.Value == this.progressBar1.Maximum)
{
MessageBox.Show("OK");
return;
}
}
}
}
http://blog.csdn.net/gisfarmer/archive/2008/07/31/2745426.aspx