运行下面代码的时候,当Label开始运行的时候,其他控件如TextBox、一个停止线程的按钮和结束窗体的按钮,全部都会失效。请问是什么原因呢?怎么解决?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace TT2
{
public partial class Form1 : Form
{
Thread t1;
int i, j; public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void btnStartThread_Click(object sender, EventArgs e)
{
lbl1.Text = "线程运行开始";
lbl1.Update();
Thread.Sleep(1000);
//textThread();
t1 = new Thread(new ThreadStart(runt1));
t1.Start();
t1.Join();
if (i == j)
MessageBox.Show("线程1完成");
} void runt1()
{
for (i = j; i < j + 50; i++)
{
lbl2.Text = i.ToString();
lbl2.Update();
Thread.Sleep(150);
}
j = i;
} private void button2_Click(object sender, EventArgs e)
{
t1.Abort();
} private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace TT2
{
public partial class Form1 : Form
{
Thread t1;
int i, j; public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void btnStartThread_Click(object sender, EventArgs e)
{
lbl1.Text = "线程运行开始";
lbl1.Update();
Thread.Sleep(1000);
//textThread();
t1 = new Thread(new ThreadStart(runt1));
t1.Start();
t1.Join();
if (i == j)
MessageBox.Show("线程1完成");
} void runt1()
{
for (i = j; i < j + 50; i++)
{
lbl2.Text = i.ToString();
lbl2.Update();
Thread.Sleep(150);
}
j = i;
} private void button2_Click(object sender, EventArgs e)
{
t1.Abort();
} private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
解决方案 »
- 关于treeview问题,节点前面的加号怎么在加载时就显示
- 强名称验证失败
- 如何通过不同网络连接广播相应IP,其他计算机接收后可建立连接?
- 寻找 VS.NET2005 中的一个控件!类似与第三方工具dotnetbar控件集里的sidebar控件!
- 类成员的 internal 访问性不解,protected 与 protected internal 有区别吗?
- C#中Class(类)和Struct(结构)有什么区别,在什么情况下会用到Struct?
- 关于datagrid的问题,困扰了好久,不知道能不能实现?谢谢!
- ado.net如何读取Access中的长二进制数据?
- 一个变量的问题
- ★一个鼠标类☆(Using C# and Win32API)
- 急求一个dataadapter.update()更新合成表的例子
- 安装duwamish出错?????
你去提这句进行调试一下啦`````
不建议自己屏蔽一些错误
{
if(lbl2.InvokeRequired)
{
LabelHandler lh = ChangeLabelText;
this.Invoke(lh,new object[]{text});
}else
{
lbl2.Text = text;
lbl.Update();
}
}然后再你的Run1 里, 删除 lbl2.Text = i.ToString(); lbl2.Update(); 直接调用 ChangeLabelText(i.ToString());