我在我的应用程序中使用任务类。这不是WPF应用程序!现在的问题是有没有从任务体在UI线程调用函数中的任何可能性,就像这样:C#.NET 4 Can Task可以在UI线程上提供通知吗?
var task = new Task(() => DoSmth(1));
task.Start();
public void DoSmth(int arg)
{
//smth
CallNotifFuncOnUIThread(() => Notify(1));
//smth ELSE
CallNotifFuncOnUIThread(() => Notify(2));//notify AGAIN
//smth ELSE
}
public void Notify(int arg)
{
progressBar1.Value = arg;
}
或者,也许有这个问题的其他解决办法?我知道BackgroundWorker类,但Tasks如何?
相关:http://stackoverflow.com/questions/5971686 – dtb
它是WPF?如果是这样,请使用['Dispatcher'](http://msdn.microsoft.com/pl-pl/library/system.windows.threading.dispatcher.aspx),即'Application.Current.Dispatcher',它将给予你的UI调度员。当然,如果你想在后台处理过程中的某个地方更新UI,如果你只是想在最后更新它,在延续中使用适当的调度器上下文。 –
这里是类似的问题的答案:http://stackoverflow.com/questions/15327717/algorithm-progress-callback/15661639#15661639使用TPL INotify接口 – semeai