【发布时间】:2014-03-03 12:50:49
【问题描述】:
我正在尝试更新绑定到 ProgressBar 的主线程上的属性。在 viewmodel 中,我有下面的代码,它不起作用。
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task task = Task.Factory.StartNew(() =>
{
DoLongRunningWork();
}).ContinueWith(_=>
{
ApplicationStatus = "Task finished!";
}, TaskScheduler.FromCurrentSynchronizationContext());
DoLongRunningWork()
{
// Alot of stuff
Task.Factory.StartNew(() =>
{
ProgressBarValue += progressTick;
}).Start(uiScheduler);
}
【问题讨论】:
-
解释
not working -
ProgressBarValue += progressTick;没有更新
-
哪个版本的框架?以及
ProgressBarValue和progressTick的变量类型是什么? -
您正在更新另一个任务中的
ProgressBarValue。它应该由 UI 线程更新。或者你应该使用SomeControl.InvokeBTW:外部任务立即完成,因为它只创建另一个任务(做实际工作)然后完成 -
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => ProgressBarValue += progressTick));在此答案中:stackoverflow.com/a/1644254/2609288
标签: c# multithreading task-parallel-library task