Question

I have the follwowing code:

var trans = conn.CreateTransaction();
trans.StringSetAsync(keyValuePairs, flags: flags);
trans.SortedSetAddAsync(KeyForAllMessages(), messages.Select(m => new KeyValuePair<RedisValue, double>(m.id, m.id)).ToArray());
trans.SortedSetAddAsync(threadKey, messages.Select(m => new KeyValuePair<RedisValue, double>(m.id, m.id)).ToArray()).GetAwaiter();
return await trans.ExecuteAsync(flags);

The above code is inside an async method named:

public async Task CacheMessagesAsync(....)

VS 2013 underlines the three trans.XXX commands and display a Warning that states:

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Although the last command

return await trans.ExecuteAsync(flags);

means that the method wait for the above commands. Why i get the warning? Does this have anything to do with the implementation of the calls of Stackexchange.Redis Api or i'm missing something about writing proper CTP code?

Was it helpful?

Solution

You're awaiting just one of the async methods. You should probably be awaiting all of the methods, in some way, so that the other three finish by the time the task representing this method finishes.

If it's important that each task finishes before the next starts, then just await them all.

If you just need to run all of them but they can all be run in parallel, then you can just pass all of them to Task.WhenAll and await that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top