下面是一个窗体程序代码,一个按钮,一个label。为什么一调试就报错:下面所示线程间操作无效: 从不是创建控件“label1”的线程访问它。不调试没错,匿名委托应该能访问包含它的类变量的啊!
private void button1_Click(object sender, EventArgs e)
{
new System.Threading.Thread(delegate()
{
this.label1.Text = "1"; System.Threading.Thread.Sleep(5000); this.label1.Text = "2";
})
.Start();
}
private void button1_Click(object sender, EventArgs e)
{
new System.Threading.Thread(delegate()
{
this.label1.Text = "1"; System.Threading.Thread.Sleep(5000); this.label1.Text = "2";
})
.Start();
}
解决方案 »
- const int Bnum = ds.Tables[0].Rows.Count;这样写提示有错,不能直接把数据库提取的数据传给常量Bnum吗?
- WebBrowser与HTMLDocument如何替换页面文字
- 二维数组的问题哦,新手,请给位赐教!
- switch 中一具case 多条件问题
- WPF如何把listbox的数据存到xml文件上?还有用了Ado.net实体数据模型,该如何读取一整张表的数据到datagrid里,是MVVM模式的
- C# 如何 实现 像VB 的 Format函数 一样的功能 ??
- 错误提示:"此文件是由另一个专案开启"
- 如何判断数据库中是否存在某一名字的表
- 怎么做一个只有密码和用户名登录的程序(如果可以保证是180分送,绝不食言)
- tcpclient 问题,求助
- 请问.net c# 中有没有类似于java nio 的机制呢?
- " @ "什么意思
{
new System.Threading.Thread(delegate()
{
this.Invoke(new MethodInvoke(delegate
{
this.label1.Text = "1"; System.Threading.Thread.Sleep(5000); this.label1.Text = "2";
}));
})
.Start();
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
new System.Threading.Thread(delegate()
{
this.Invoke(new MethodInvoker(delegate
{ this.label1.Text = "1"; System.Threading.Thread.Sleep(5000); this.label1.Text = "2";
}));
})
.Start(); } }
}