简体   繁体   中英

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).

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.

To wait for multiple task, you can your Task.WaitAll .

Task.WaitAll(Data,Data2);

To update on UI, you can use synchronizationcontext .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM