1

In my WPF application, i am calling relaycommand

    private void AutoRun(object parameter)
    {
        for(int i=0;i<10;i++)
        {
        MoveLotCommand.Execute(0);

        }

    }

which inturns call another relay command

  private void MoveLot(object parameter)
    {
        //Some Code

            var Task = StartLotProcessing(currentAssemblyPlant);

        }
    }

and this relay command will call another async function

    async Task StartLotProcessing(int currentAssemblyPlant)
    {
        await Task.Delay(5000);
        var nextAssemblyPlant = currentAssemblyPlant + 1;

        //Process after await
    }

The issue is, my code after 'await Task.Delay(5000)' does not executes until my AutoRun() function execution is completed.

I am trying to execute the await async code for each variable in my for loop.

Any help would be appreciated and sorry for my explanation.

amandeep
  • 19
  • 3
  • If i follow your explanation, i think your problem is how await in a loop will work. https://stackoverflow.com/questions/19431494/how-to-use-await-in-a-loop – Andy Jan 31 '19 at 19:54

2 Answers2

2

You are not calling the method correctly : you are missing the await operator to indicate that you have to wait, otherwise you effectively schedule the task but do not wait for it. The task is scheduled in a synchronous fashion, hence when your method returns.

Change your call to this, and the issue should go away:

await StartLotProcessing(currentAssemblyPlant);
Yennefer
  • 5,704
  • 7
  • 31
  • 44
1

The problem appears to be that you are calling StartLotProcessing asynchronously but not waiting for the task to complete before moving on.

Ideally you would call use async/await the whole way up the stack:

await MoveLotCommand.Execute(0);
...
await StartLotProcessing(currentAssemblyPlant);

If you're unable to do that, then you should call

var task = StartLotProcessing(currentAssemblyPlant);
...
task.Wait();
NextInLine
  • 2,126
  • 14
  • 23