The await keyword is a new keyword in C# 5.0 which, in tandem with async keyword, allows us to easily author methods which execute asynchronously in regards to the calling code. In a previous post I’ve shown a certain issue you should look out for when using the async keyword. In this post we’ll check a similar issue with the await keyword.
The await Trap
Let’s remember our test code from the previous post:
class AsyncClass
{
public void Process(string s)
{
if (s == null) throw new ArgumentNullException("s");
ProcessCore(s);
}
public Task ProcessAsync1(string s)
{
if (s == null) throw new ArgumentNullException("s");
return Task.Run(() => ProcessCore(s));
}
public async Task ProcessAsync2(string s)
{
if (s == null) throw new ArgumentNullException("s");
await Task.Run(() => ProcessCore(s));
}
private void ProcessCore(string s)
{
for (int len = 1; len <= s.Length; len++)
{
Thread.Sleep(1000); // This task is very complicated!!
Console.WriteLine(s.Substring(0, len));
}
}
}
What do you think the following code does?
var asyncTest = new AsyncClass();
await asyncTest.ProcessAsync2(null);
Well, you’re probably saying that this time we are actually awaiting the method to complete so it must throw an ArgumentNullException! And, you’re correct – this is indeed the result of the above code. But how about the following code?
var asyncTest = new AsyncClass();
asyncTest.ProcessAsync2(null).Wait();
Hmmmmm… Isn’t this exactly the same as before? Won’t this throw an ArgumentNullException as well? No! It won’t.
Read more: Stiller on Software
QR:
0 comments:
Post a Comment