Why call WaitOne if you're just calling End right after? Doesn't End block?
Trying to understand this MSDN sample but I'm confused about these lines:
IAsyncResult res开发者_如何学JAVAult = Dns.BeginGetHostEntry(args[0], null, null);
Console.WriteLine("Processing request for information...");
// Wait until the operation completes.
result.AsyncWaitHandle.WaitOne();
// The operation completed. Process the results.
try
{
// Get the results.
IPHostEntry host = Dns.EndGetHostEntry(result);
For example, why even use asynchronous programming if you're just going to block the thread until the task completes anyway? Also, what good does calling WaitOne
do? Doesn't the End function automatically block until the operation completes?
Pretty confused over here..
Yes, EndGetHostEntry
blocks until the operation has completed. I suspect it's just a bad example, to be honest - possibly it's trying to show you two different ways of waiting until the operation has completed, but by putting them in the same example it's just added to confusion.
Likewise it would certainly have been more useful if it had included a comment of "// do more work here" or something like that.
It sounds like your understanding of the API is fine, and you should probably just ignore the example - or add a note at the bottom in the "user content" section to mention all this.
精彩评论