首页 新闻 会员 周边

c#多线程窗口假死的问题

0
悬赏园豆:30 [待解决问题]

private string status = "Waiting for command";

private void sendme(data1.dat2)
{
try
{

tst1 = true;
timeRemaining = int.Parse(time);
System.Threading.Thread dThread = new System.Threading.Thread(new System.Threading.ThreadStart(countDown));
dThread.IsBackground = true;
dThread.Start();

}
catch
{
bAttacking = false;
MessageBox.Show("Connection error");
}

private void displayStatus()
{
for (; ; System.Threading.Thread.Sleep(1000))
{
label7.Text = status;
}
}
private void countDown()
{
for (; ; System.Threading.Thread.Sleep(1000))
{
try
{
if (tst1)
{
timeRemaining--;
status = string.Format("has {0} time remainging", timeRemaining);
if (timeRemaining <= 0)
{
tst1 = false;
break;
}
}
}
catch
{

}

}

 

这按钮点击后进行tst1的时间,并开始倒数计时,到时候点击按钮后,窗口出现假死了,求怎么解决

zerofeng的主页 zerofeng | 初学一级 | 园豆:5
提问于:2014-06-26 16:58
< >
分享
所有回答(5)
0

看不懂这代码```

吴瑞祥 | 园豆:29449 (高人七级) | 2014-06-26 17:01

就是点击一个按钮,进行下载数据,并在lable中显示现在所需时间的到计时,但是点击下载后,就会出现窗口假死

支持(0) 反对(0) zerofeng | 园豆:5 (初学一级) | 2014-06-26 17:04
0

用 BackgroundWork 替代你的 Thread

Launcher | 园豆:45045 (高人七级) | 2014-06-26 17:07
0

displayStatus这个方法在哪儿调用的?

幻天芒 | 园豆:37175 (高人七级) | 2014-06-26 17:27

System.Threading.Thread dThread = new System.Threading.Thread(new System.Threading.ThreadStart(displayStatus));
dThread.IsBackground = true;
dThread.Start();

支持(0) 反对(0) zerofeng | 园豆:5 (初学一级) | 2014-06-26 17:30

@zerofeng: 你直接用个Timer算了。

支持(0) 反对(0) 幻天芒 | 园豆:37175 (高人七级) | 2014-06-26 17:45
0

代码没完整吧 displayStatus  thread 你的代码是countDown 还有 复制代码 有格式 这样看起来舒服些

 

线程 应该不会卡的 如果卡 更新ui线程的 控件 可以用invoke 赋值

s_p | 园豆:138 (初学一级) | 2014-06-26 22:06
0

private void displayStatus()
{
for (; ; System.Threading.Thread.Sleep(1000))
{
label7.Text = status;
}
}

这个你执行不出错误提示?按你说的,对label7的访问将不在界面线程,VS肯定会出错误提示的,

hailants | 园豆:750 (小虾三级) | 2014-06-28 22:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册