0

I have two tasks, one of which I would like to complete before the next one begins. I have tried turning these into a task and using task.Wait() but they are still executed at the wrong time. Using a short sleep works but I would like to avoid sleeping.

private async void BuildNextData(object sender, RoutedEventArgs e)
{
    try
    {
        await this._LocalCluster.DataItem(this._LocalSceneIID.IID.Value, ".Data.DataPopupNext", null, null, null);

        System.Threading.Thread.Sleep(500);

        object o = await GetVentuxIndex(".Data.DataPopupCurrentIndex");
        Workspace.This.MainJsonBond.SelectedfilteredPopupWorries = Workspace.This.MainJsonBond.filteredPopupWorries[Int32.Parse(o.ToString())];
    }
    catch (Exception ex)
    {

    }
}
Dev
  • 1,780
  • 3
  • 18
  • 46
  • 2
    do you have access to these methods to just make them synchronous? That's what you're asking to do anyhow. – oppassum Jan 31 '18 at 13:32
  • 5
    That's exactly the way you would do it, except without the `Sleep()`. `await` means, "wait for this to complete before continuing". If that's not happening, then there's a problem in your `DataItem` method. Maybe you can show us what that is doing. – Gabriel Luci Jan 31 '18 at 13:33
  • I have no control over the DataItem it is a method in a Host application in which I am sending and receiving data – Dev Jan 31 '18 at 13:35
  • 2
    If that "sending and receiving" is not fully complete once it stops awaiting, then you will have to take that up with whoever is responsible for that host application. There isn't really anything you can do. That is not the way awaitable tasks are supposed to work. – Gabriel Luci Jan 31 '18 at 13:37
  • You don't need `Thread.Sleep`. awaits execute one after another so there is no way that one can run when the previous one is not finished. The only reason why it might not work is because either `_LocalCluster.DataItem` or `GetVentuxIndex` is not properly written to support async calls. – FCin Jan 31 '18 at 13:39
  • 4
    @GabrielLuci That's good enough to write as the answer, IMHO :-) – Equalsk Jan 31 '18 at 13:39
  • True enough. :) – Gabriel Luci Jan 31 '18 at 13:43
  • Ok, thanks, guys after reading your comments I did F12 to Go to definition and it took me to the Library of the Host application and I realised that the last parameter is the state of the async method. `public Task DataItem(IID iid, string dataItem, object value, long? when, object asyncState);` – Dev Jan 31 '18 at 13:45
  • `asyncState` can be anything. Their documentation should tell you what it should be. I'm more curious about the `when` parameter. Are they allowing you to schedule when the task should be executed? It's possible the method is just queuing up the job rather than executing it immediately. But only their documentation can tell you that. – Gabriel Luci Jan 31 '18 at 13:48
  • Thanks, @gabriel Luci setting the last parameter to true didn't work as expected but I will check out the documentation to further understand the method usage. – Dev Jan 31 '18 at 15:02

1 Answers1

3

That's exactly the way you would do it, except without the Sleep(). await means, "wait for this to complete before continuing".

If the "sending and receiving" that the DataItem method is doing is not fully complete once it stops awaiting, then that's the fault of the code in that method. You will have to take that up with whoever is responsible for that host application. There isn't really anything you can do. That is not the way awaitable tasks are supposed to work.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84