Make your own free website on

[an error occurred while processing this directive]

Howto create a new thread and download asynchronously

In the latest API-Guide version, the Internet Update function performs its task asynchronously. The WinInet-functions provide a way to download files asynchronously, without having to create a second thread, but after weeks of trying to find out how these functions worked, I decided to create a new thread and perform the Internet tasks synchronously in that new thread.

Multi-threading without API is VERY difficult! Without API, you have to create a server application and you need tons of  code to get it working.
With API, we need three lines of code to call a function asynchronously. Here's how we did it:

Create a new project and add a module to it.
Place this code in the module:

Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub AsyncTest()
Sleep 10000
End Sub

If we would call the AsyncTest-function like a normal Visual Basic function, the program would be completely locked for 10 seconds. However, if we call this function asynchronously, we won't even notice that the function is "sleeping".

Dim hThread As Long, hThreadID As Long
hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncTest, ByVal 0&, ByVal 0&, hThreadID)
CloseHandle hThread

The following part is quite simple. Replace the sleep function with the download code, and your asynchronous function is complete.
We have an example on our site that shows you how to use the WinInet-functions to download a file. This example can be found here.

Copyright © 1998-2000, The KPD-Team.
Send mail to with comments about this web site.
This site is located at