README.md

December 4, 2022 ยท View on GitHub

Easy multithread in VB (Updated)

Description

Its actualy quite easy to make several threads in VB. All you need is one (1), API call and you're on your way.

More Info

Submitted On2002-02-15 10:35:32
ByThomas Raben
LevelIntermediate
User Rating4.4 (31 globes from 7 users)
CompatibilityVB 4.0 (32-bit), VB 5.0, VB 6.0
CategoryWindows API Call/ Explanation
WorldVisual Basic
Archive FileEasy_multi553822152002.zip

Source Code

To make serveral threads in VB, you need a single API call and a few constants (this code should be placed in a module) :

Public Const CTF_COINIT = &H8
Public Const CTF_INSIST = &H1
Public Const CTF_PROCESS_REF = &H4
Public Const CTF_THREAD_REF = &H2

Declare Function SHCreateThread Lib "shlwapi.dll" (ByVal pfnThreadProc As Long, pData As Any, ByVal dwFlags As Long, ByVal pfnCallback As Long) As Long

Next is to make sub for the thread (it will not work for a private).

public sub myNewThread()
dim i as integer

for i = 0 to 99
debug.print "message from a new thread (" & i & ")"
next i
end sub

Last but not least, all you need to do, is invoke the new thread: SHCreateThread AddressOf myNewThread, ByVal 0&, CTF_INSIST, ByVal 0&

That's it.

You have a new thread (make sure to end the sub before you exit the program or vb will crash.)