views:

99

answers:

3

Hello, I found an example for async ftp upload on msdn which does the following (snippet):

        // Asynchronously get the stream for the file contents.
        request.BeginGetRequestStream(
            new AsyncCallback (EndGetStreamCallback), 
            state
        );

        // Block the current thread until all operations are complete.
        waitObject.WaitOne();

The thing what I do not understand here is, which sense does asynchronous IO make if the thread is blocked anyway with an explicit waithandle. I always thought the advantage of asynchronous IO was that the user/programm does not have to wait.

+3  A: 

It's just an example.

In a Real World Application (TM) your code could be running the GUI thread. And we all know that blocking the GUI thread is killer when it comes to user experience.

When the async operation finished, it would call some sort of a notifier in your GUI, which allows the user to do other stuff while waiting for the transfer to finish.

Kent
+1  A: 

It could be only done for the example - using minimum code needed to show how it's done.

What you do get from the code above is that the main thread does not use any CPU while waiting for the operation to end - which could be logical if you're using GUI.

Dror Helper
+1  A: 

Yes, it does not make much sense. I presume the point of the example is to demonstrate the syntax of calling it asynchronously, but you're right, if you're just going to block, you defeat the point of the async call in the first place.

Brian