使用线程的问题 [英] problem with using thread
本文介绍了使用线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi
i想要计数0到1000并使用线程在文本框中写入
这是我的代码,但它没有做,几秒后它转到debugger.break();
我该怎么做才能正确运行?
Hi
i want to count 0 to 1000 and write in textbox using thread
this is my code but its not doing and after a few second it go to debugger.break();
what should i do to it run correctly?
private void loop()
{
while (check == true)
{
Dispatcher.BeginInvoke(() =>
{
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(100);
txtResult.Text = i.ToString();
}
});
}
}
private void btnClick_Click(object sender, RoutedEventArgs e)
{
check = true;
ThreadStart st = new ThreadStart(loop);
Thread s = new Thread(st);
s.Start();
}
尊重
With Respect
推荐答案
现在,你继续调用一个调度程序线程上的方法从0到1000循环,并且它在该循环中阻止调度程序线程100毫秒。你不应该阻止调度程序线程,只阻止你的循环线程。另外,我想你可能想要一个if
而不是而
。
试试这个:
Right now, you keep invoking a method on the dispatcher thread that loops from 0 to 1000, and it blocks the dispatcher thread 100 milliseconds while it is in that loop. You should not be blocking the dispatcher thread, only block your loop thread. Also, I think you probably want anif
instead of awhile
.
Try this:
private void loop()
{
if (check)
{
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(100);
Dispatcher.Invoke(() =>
{
txtResult.Text = i.ToString();
}
}
}
}
这篇关于使用线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文