VB Code:
  1. 'Using the CreateThread function in Visual Basic
  2. 'is very risky! VB5 is 'kinda' stable, but VB6
  3. 'applications will probably crash when you
  4. 'use the CreateThread function.
  5.  
  6. 'In a form
  7. 'Add a command button to the form
  8. Private Sub Command1_Click()
  9.     'KPD-Team 1999
  10.     'URL: [url]http://www.allapi.net/[/url]
  11.     'E-Mail: [email][email protected][/email]
  12.     'After you click this button, try to move the window
  13.     'You will see that the AsyncThread-function was executed asynchronously
  14.     hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
  15.     CloseHandle hThread
  16. End Sub
  17. Private Sub Form_Unload(Cancel As Integer)
  18.     'If the thread is still running, close it
  19.     If hThread <> 0 Then TerminateThread hThread, 0
  20. End Sub
  21. 'In a module
  22. Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  23. 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
  24. Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
  25. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  26. Public hThread As Long, hThreadID As Long
  27. Public Sub AsyncThread()
  28.     'Let this thread sleep for 10 seconds
  29.     Sleep 10000
  30.     hThread = 0
  31. End Sub