2

THIS WORKS:

    public static async Task<T> GetClientDataAsync(string id)
    {
        var task = dynamoDBClient.GetItemAsync(new GetItemRequest
            {
                TableName = "x",
                Key = new Dictionary<string, AttributeValue>
                {
                    { "ID", new AttributeValue { S = id } }
                }
            }).Result;

        return task.IsItemSet ? task.Item : null;
    }

THIS DOES NOT:

    public static async Task<T> GetClientDataAsync(string id)
    {
        var task = await dynamoDBClient.GetItemAsync(new GetItemRequest
            {
                TableName = "x",
                Key = new Dictionary<string, AttributeValue>
                {
                    { "ID", new AttributeValue { S = id } }
                }
            });

        return task.IsItemSet ? task.Item : null;
    }

When calling the method with something like var result = GetClientDataAsync(...).Result The second one hangs forever.

luis
  • 2,067
  • 2
  • 13
  • 21
  • 1
    Please don't post duplicates of your own posts: http://stackoverflow.com/questions/32239661/await-vs-task-result-in-a-async-method – Rob Aug 27 '15 at 04:18
  • this is not a duplicate. This addresses a different issue. – luis Aug 27 '15 at 04:18
  • Original: "in my case for some reason only the second works. The first one seems to never end." - This question: "The second one hangs forever." It's the same question. You received answers on your other post and were also redirected to the duplicate question, which does answer your question – Rob Aug 27 '15 at 04:20
  • 1
    what is the return type of `GetItemAsync`? – NeddySpaghetti Aug 27 '15 at 04:23
  • "@luis: Lacking any other information, the only answer I see to that is that it's not actually working in the await case. You just mistakenly think it does because the method itself returns. But the task being awaited likely does not complete either way. If you want an answer to that (which is a different question than the one you asked), you need to post a new question, stating that clearly, and providing a good, minimal, complete code example that reliably reproduces the problem. " – luis Aug 27 '15 at 04:23
  • I wrote T because I wanted to avoid that Part, it's a custom type that before being returned a new CustomType(of a attribute map) – luis Aug 27 '15 at 04:28
  • I feel that the return type is unimportant though, i might be wrong. – luis Aug 27 '15 at 04:28

1 Answers1

1

Both functions should work. If the one with await is hanging forever it indicates that there is a SynchronizationContext present that tries to synchronise all tasks to a single thread and that thread is hanging due to something that is happening outside of this function.

That is a very common case if the function is called from the context of WPF or Windows Forms and the result of the function is fetched by .Result or the completion is awaited by using the .Wait function.

Check System.Threading.SynchronizationContext.Current for the presence of a synchronization context.

You should try to avoid mixing synchronous waiting (.Wait, .Result) and asynchronous waiting (await). Mixing both causes deadlocked situations especially when dealing with a SynchronizationContext very easily.

So I suggest you get rid of the .Result and call your function like this:

await GetClientDataAsync(...)

Or you get rid of the SynchronizationContext by forcing the function into the thread pool (that does not have a SynchronizationContext like so:

Task.Run(() => GetClientDataAsync(...)).Result

Task.Run has a proper overload to do that that automatically unwraps the inner task.

Nitram
  • 6,486
  • 2
  • 21
  • 32