Environment: Visual C++ 6.0 with Service Pack 3, Windows 9x and Windows NT 4.0 with Service Pack 3
This sample demonstrates how to use the ::URLDownloadToCacheFile function to download a file from the Internet without blocking the user interface. The use of the ::URLDownloadToFile function is similar. The sample is an MFC dialog-based application that creates a worker thread to perform the download task.
The user is supposed to enter the URL in the URL edit box and then press the Download button. The CUrlFileDlg::OnOK will be called since the ID of the Download button is IDOK. The CUrlFileDlg::OnOK retrieves the URL string from the URL edit box and checks its validity using the ::IsValidURL function. The second parameter of the ::IsValidURL function is expected to be of the LPCWSTR type. Here, the T2CW conversion macro is used. For more information about MBCS/Unicode Conversion Macros, see the MFC Technical Note "TN059: Using MFC MBCS/Unicode Conversion Macros". If the URL is valid (but it is not necessarily correct), a worker thread is created by calling ::AfxBeginThread. The controlling function for the worker thread is a static member function of CUrlFileDlg - CUrlFileDlg::Download, which calls ::URLDownloadToCacheFile and posts a user-defined message WM_USER_ENDDOWNLOAD to the dialog box after ::URLDownloadToCacheFile returns. The message-handler function for WM_USER_ENDDOWNLOAD waits until the worker thread terminates, then deletes the CWinThread object. It also displays the name of the downloaded file. The last parameter of ::URLDownloadToCacheFile is the address of the caller's IBindStatusCallback interface. ::URLDownloadToCacheFile calls this interface's IBindStatusCallback::OnProgress method on a connection activity, including the arrival of data. Implementing IBindStatusCallback::OnProgress allows a caller to implement a user interface or other progress monitoring functionality. It also allows the download operation to be canceled by returning E_ABORT from the IBindStatusCallback::OnProgress call. The implementation of the IBindStatusCallback interface is the CBSCallbackImpl class. The CBSCallbackImpl::OnProgress sends a user-defined message WM_USER_DISPLAYSTATUS to the dialog box to display the progress messages. It also uses the ::WaitForSingleObject function to check the current state of the event object, which is set to the signaled state when the user has pressed the Stop button (the same button as the Download button) during downloading. If the event object is in the signaled state, the CBSCallbackImpl::OnProgress returns E_ABORT to cancel the download operation.
Note that the following setting affects the behavior of ::URLDownloadToCacheFile just like
the Internet Explorer browser.
Control Panel/Internet/General/Temporary Internet files/Settings/Check for newer versions of stored pages