none
What condition causes Task.Wait(TimeSpan) to return true? RRS feed

  • Question

  • I'm calling an async method on a remotely hosted service, i.e. proxy.SomeAsyncMethod(). If I await the method it returns within a few milliseconds however if I instead call proxy.SomeAsyncMethod().Wait(timespan) it returns false every time regardless of the timespan used. My feeling is there's some issue with the implementation of the async method itself in which the returned Task's state isn't being set correctly but I'm not sure what specific state the Wait method is looking for to return true. Does anyone know what this might be or have an alternate theory?
    • Edited by Dane Vinson Friday, October 23, 2015 7:13 PM
    Friday, October 23, 2015 7:12 PM

Answers

  • Hi Dane Vinson,

    There are some different between wait and await. See below article.
    http://stackoverflow.com/questions/13140523/await-vs-task-wait-deadlock

    >>Using Wait never returns within the timespan allotted no matter what the timespan is.

    I guess it will cause deadlock in wait scene. So you need to contact your service provider and get the source code of GetStuff() method. 

    Best Regards,
    Li Wang 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Dane Vinson Thursday, October 29, 2015 4:42 PM
    Thursday, October 29, 2015 2:34 AM
    Moderator

All replies

  • Hi Dane Vinson,

    Wait method of Task waits for the Task to complete execution within a specified time interval. If f the Task completed execution within the allotted time, it will return true, otherwise return false.

    You could try below code to understand this method.

    WebClient client = new WebClient();
    Task<byte[]> task = client.DownloadDataTaskAsync("http://www.bing.com");
    Console.Write(task.IsCompleted);
    //it will return true if task finished in 1 second. On my side. it will return true
    //if I change 1 to 0.1 it will return false on my side if (task.Wait(TimeSpan.FromSeconds(1))) { Console.Write(task.IsCompleted); }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 26, 2015 3:06 AM
    Moderator
  • That was my understanding as well but is clearly not the entire picture. As I said the method in question returns every time within a couple milliseconds when I call it using await but times out and returns false every time when using Wait regardless of the timespan used.
    Monday, October 26, 2015 3:16 PM
  • Hi Dane Vinson,

    Do you mean use Wait method will spend more time? If you want a deep understanding of Wait method. You could reference source of Wait method on below link.
    http://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Task.cs,855657030ba22f78

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 27, 2015 7:59 AM
    Moderator
  • Wait doesn't just take more time it ALWAYS times out. Using await returns quickly every time. Using Wait never returns within the timespan allotted no matter what the timespan is.
    Tuesday, October 27, 2015 3:52 PM
  • Hi Dane Vinson,

    >>Using Wait never returns within the timespan allotted no matter what the timespan is.

    Have you tried the sample code which I provided? That works fine on my side. Could you share your code? Do you use below constructor to build TimeSpan object?

    public TimeSpan(long ticks)

    1 tick means 100-nanosecond units.  1ns = 0.000 000 001s. If you want to wait 1s. You need to use below code.

    task.Wait(new TimeSpan(10000000))
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 28, 2015 3:22 AM
    Moderator
  • The code you provided will absolutely work, it's all local. The case I've encountered is occurring when I call a service I don't own. I was hoping to find the reason for the behavior so I cold alert the vendor and help them solve the issue but there's no code for me to share with you as I don't own it. I use the TimeSpan.FromSeconds static method to construct my TimeSpan but the resulting object is the same. This is not an issue in understanding what unit of measurement TimeSpan is in.

    This returns within a couple milliseconds:

    var return = await service.GetStuff();

    While this always returns false regardless of the TimeSpan used:

    var task = service.GetStuff();
    var success = task.Wait(TimeSpan.FromSeconds(10));
    Further, task.Result is always null at the end. Something is happening at the service level. I'd have expected both these methods to work correctly but it really looks like some type of state on the Task object itself is not being set correctly though I have no idea what that could be as I don't know of any such state.

    Wednesday, October 28, 2015 3:18 PM
  • Hi Dane Vinson,

    There are some different between wait and await. See below article.
    http://stackoverflow.com/questions/13140523/await-vs-task-wait-deadlock

    >>Using Wait never returns within the timespan allotted no matter what the timespan is.

    I guess it will cause deadlock in wait scene. So you need to contact your service provider and get the source code of GetStuff() method. 

    Best Regards,
    Li Wang 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Dane Vinson Thursday, October 29, 2015 4:42 PM
    Thursday, October 29, 2015 2:34 AM
    Moderator
  • Yes, that's sort of my take as well. I was hoping to understand what the deadlock issue might be so I could reach out to them with a suggestion on how to fix it.
    Thursday, October 29, 2015 4:42 PM