The amount of time it takes an application to access an Internet resource depends on a number of factors, such as the connection being used, the server on which the resource is located, and the number of users trying to access the resource. For applications that download multiple resources or handle multiple tasks (including one or more downloads), waiting for each download to complete before moving on to the next task can be extremely inefficient. To avoid waiting, many of the Microsoft® Win32® Internet functions provide a way to perform tasks asynchronously.
In asynchronous mode, an application can execute any Win32 Internet function that includes a context value as one of its parameters and can continue to execute other commands or functions while it waits for the Win32 Internet function to complete its task. While the task is completing, a status callback function provided by the application is notified about the progress of the task and when it has completed. At this time, the status callback function can call other functions or perform any other needed tasks that were dependent on the task's completion.
So what type of benefits can you get by using the Win32 Internet functions asynchronously? You can, for example:
Your application can start connecting to multiple Internet resources at the same time and download them as they become available.
An application using the Win32 Internet functions asynchronously does not have to wait until the request is completed, so the application is free to do other tasks that are not dependent on the request, thus improving the application's overall performance.
The status callback function receives notifications while it is processing a request. If needed, your application can take the information provided by that status callback function to keep the user informed about the progress of the operation or to interrupt requests that are taking too long to complete.
Let's say your application needs to download coffee prices from the Downfall Coffee & Tea and the Fourth Coffee sites and compare prices. The Fourth Coffee site usually has a slower response time, so your application should download the information from Downfall Coffee & Tea first.
Two versions of the application are developed. One works synchronously, downloading the prices from the Downfall Coffee & Tea site first and then the prices from the Fourth Coffee site afterward. The second works asynchronously, sending requests to both sites and downloading the prices when they become available.
The following table illustrates what would happen if the Fourth Coffee site was faster on a particular day.
Event | Synchronous version | Asynchronous version |
---|---|---|
Start | Send request to Downfall Coffee & Tea | Send requests to Downfall Coffee & Tea and Fourth Coffee |
Request from the asynchronous version to Fourth Coffee completed | Waiting | Download prices from Fourth Coffee |
Request to Downfall Coffee & Tea completed | Download prices from Downfall Coffee & Tea | Download prices from Downfall Coffee & Tea |
After Downfall Coffee & Tea's prices are downloaded | Send request to Fourth Coffee | Compare prices |
Asynchronous version's comparison completed | Waiting | Operation complete |
Request from the synchronous version to Fourth Coffee completed | Download prices from Fourth Coffee | n/a |
After Fourth Coffee's prices are downloaded | Compare prices | n/a |
Synchronous version's comparison completed | Operation complete | n/a |
Another example would be a Web browser such as Microsoft® Internet Explorer. When the browser downloads a page, it often needs to download other resources, such as images and sound files. In asynchronous mode, the page and its associated resources can be requested simultaneously and downloaded as they become available, instead of requesting and downloading the page and each resource one at a time.
The following lists contain links to topics related to using the Win32 Internet functions asynchronously.
Tutorials
Functions needed to set up asynchronous operation
Win32 Internet functions that can be used asynchronously
Note The FtpCreateDirectory, FtpRemoveDirectory, FtpSetCurrentDirectory, FtpGetCurrentDirectory, FtpDeleteFile, and FtpRenameFile functions use the context value provided in the call to the InternetConnect function.