How to wait till ShowViewModel<> gets called

In the below code I am trying to wait till a view appears „xyzViewModel“ and then, publishes a message to the same view model „xyzViewModel“ that I have just started it using ShowViewModel

I tried to use the code posted below, but non of them waits till the view model start. What happens is, either

Task.Factory.StartNew(() =>

Or Task.Run(() =>

Gets called, then immediately,

            new ExitOrstartxyzPublisher().PublishMessage(this, true);

What I am want to do is, to wait till the xyzVeiwModel called then

                new ExitOrstartxyzPublisher().PublishMessage(this, true);

Should be called

Please let me know how can I do that correctly

code

              private async Task ExitOrStartxyzVM()
    {
        /* await Task.Factory.StartNew(() =>
         {
         });*/
        await Task.Run(() =>
        {
             ShowViewModel<xyzViewModel>();
        });
        new ExitOrstartxyzPublisher().PublishMessage(this, true);

    }

1 answer

  • answered 2019-02-10 12:34 CGPA6.4

    You can use this code to wait until the current Task finishied

    Task.Run(async () => {await ShowViewModel<xyzViewModel>(); }).Wait();
    

    Shortly after completing above Task control will move to the next line.

    new ExitOrstartxyzPublisher().PublishMessage(this, true);
    

    Or you can remove async & await

    Task.Run(() => {ShowViewModel<xyzViewModel>(); }).Wait();