[英]Backgroundworker for wpf C#
I need to execute two methods simultaneously, only after completion of this next thing should continue, the problem i have a UI which throws me error if use task (Its a WPF appliation).我需要同时执行两个方法,只有在完成下一步之后才应该继续,问题是我有一个 UI,如果使用任务(它是 WPF 应用程序)会引发错误。
Task Data= Task.Factory.StartNew(() => Readdetails());
Task Data2= Task.Factory.StartNew(() => ReadProcedure());
Data.Wait();
Data2.Wait();
but i am getting Error(must-create-dependencysource-on-same-thread-as-dependencyobject).但我收到错误(必须创建依赖源在同一线程作为依赖对象)。
something like this i need.我需要这样的东西。
private void ReadProcedure()
{
this.Pro = //some stuff;
}
private void ReadProcedure2()
{
this.Pro2 = //some stuff;
}
if(this.pro!=null && this.pro2!=null)
{
//other things to carry out.
}
I tried using Dispacter but it seems it's still not working.我尝试使用 Dispacter,但似乎仍然无法正常工作。
To wait for multiple task, you can your Task.WaitAll .要等待多个任务,您可以使用Task.WaitAll 。
Task.WaitAll(Data,Data2);
To update on UI, you can use synchronizationcontext .要在 UI 上更新,您可以使用同步上下文。
Task UITask= Task.Factory.StartNew(() =>
{
if(this.pro!=null && this.pro2!=null)
{
//other things to carry out.
}
}, TaskScheduler.FromCurrentSynchronizationContext());
You can combine above two, using TaskFactory.ContinueWhenAll .您可以使用TaskFactory.ContinueWhenAll将上述两者结合起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.