Posted: Sun Mar 27, 2005 12:41 pm
Prior to discussing asynchronous operations over the Internet, let's touch on one important part of data transferring. Regardless of the established session type (synchronous or asynchronous), users of your application probably will appreciate it if it somehow indicates that lengthy operations are advancing and still alive. WinInet provides a callback mechanism for such purposes. An application may register such a callback to retrieve asynchronous notifications about the desired operation's progress. You should use the following function:
typedef void (CALLBACK *INTERNET_STATUS_CALLBACK) (
HINTERNET hInternet,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength
);
INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
HINTERNET hInternet,
INTERNET_STATUS_CALLBACK lpfnInternetCallback
);
InternetSetStatusCallback takes a pointer to the application-defined function, which then is called by WinInet when progress is made on the specified operation. Saying it briefly, all you need to do is to define the following function:
void CALLBACK YourInternetCallback(
HINTERNET hInternet,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength
);
All parameters are described in detail in online documentation, so we won't copy it here. Let's just highlight several useful points. The second parameter of the callback function is an application-defined context for the hInternet handle. This gives you a convenient way to implement different behavior for each context value; for example, for uploading and downloading data. Another thing to be noted is the dwInternetStatus parameter. You will use its values to inform the user about actual operation progress and to detect the moment when the asynchronous operation is completed. Thus, such a callback may look like the following:
void CALLBACK InternetCallback(HINTERNET hInternet,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength)
{
switch (dwContext)
{
case X:
...
switch (dwInternetStatus)
{
...
case INTERNET_STATUS_CONNECTED_TO_SERVER:
// Notify the user...
...
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
// Asynchronous request was completed
...
break;
case INTERNET_STATUS_RESPONSE_RECEIVED:
// Process this event...
...
break;
...
}
break;
}
}
The Compact Framework still does not provide the same mechanism as C++ does, but we will see what C# offers for asynchronous calls in the next section.
This is a code ok? i know c++ now shut the **** up you fatuous poser
typedef void (CALLBACK *INTERNET_STATUS_CALLBACK) (
HINTERNET hInternet,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength
);
INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
HINTERNET hInternet,
INTERNET_STATUS_CALLBACK lpfnInternetCallback
);
InternetSetStatusCallback takes a pointer to the application-defined function, which then is called by WinInet when progress is made on the specified operation. Saying it briefly, all you need to do is to define the following function:
void CALLBACK YourInternetCallback(
HINTERNET hInternet,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength
);
All parameters are described in detail in online documentation, so we won't copy it here. Let's just highlight several useful points. The second parameter of the callback function is an application-defined context for the hInternet handle. This gives you a convenient way to implement different behavior for each context value; for example, for uploading and downloading data. Another thing to be noted is the dwInternetStatus parameter. You will use its values to inform the user about actual operation progress and to detect the moment when the asynchronous operation is completed. Thus, such a callback may look like the following:
void CALLBACK InternetCallback(HINTERNET hInternet,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength)
{
switch (dwContext)
{
case X:
...
switch (dwInternetStatus)
{
...
case INTERNET_STATUS_CONNECTED_TO_SERVER:
// Notify the user...
...
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
// Asynchronous request was completed
...
break;
case INTERNET_STATUS_RESPONSE_RECEIVED:
// Process this event...
...
break;
...
}
break;
}
}
The Compact Framework still does not provide the same mechanism as C++ does, but we will see what C# offers for asynchronous calls in the next section.
This is a code ok? i know c++ now shut the **** up you fatuous poser