我有以下代码:
progressBar1.Minimum = 0;
progressBar1.Maximum = Results.Count;
foreach (MyClass cls in Results)
{
progressBar1.Value += 1;
// Go to DB and get large quantity of data
cls.GetHistoryData();
}我想要做的是将处理转移到另一个线程,以便progressBar1正确更新。我发现了一个article,它暗示我应该能够在进度条上使用Invoke方法,但似乎没有。
发布于 2010-11-19 18:38:54
如果将进度条绑定到数据属性,则不需要手动切换线程上下文。WPF绑定引擎将自动为您完成此操作。
<ProgressBar Value={Binding Progress} />然后在你的线程中:
foreach (MyClass cls in Results)
{
// databinding will automatically marshal to UI thread
this.Progress++;
cls.GetHistoryData();
}在大多数情况下,这比使用Dispatcher.Invoke或BackgroundWorker自己编组要干净得多,也不容易出错
发布于 2010-11-19 18:36:45
你应该检查BackgroundWorker类。它支持进程,并正确处理线程之间的通信。
发布于 2010-11-19 18:37:25
您可以像这样启动一个新线程:
Thread t1 = new Thread(methodnametocall);
t1.start();
void methodnametocall()
{
this.Invoke((MethodInvoker)delegate
{
control to update;
}
});https://stackoverflow.com/questions/4224146
复制相似问题