-2

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.

Tilak
  • 30,108
  • 19
  • 83
  • 131
Usha
  • 1

1 Answers1

0

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.

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • Add more details to your question, including code snippet to be executed on UI, and background thread. – Tilak Feb 02 '17 at 18:59