Date: 02-16-2022
Return to Index
created by gbSnippets
'Windows supports the creation of multiple threads, which can be useful
'in many applications.
'Primary Code:
'The basic code is the same for in the example of a single thread (http://gbl_00336):
'Create a thread Function to run as the thread
Thread Function NewThread (ByVal x As Long) As Long
'... thread code goes here
End Function
'Create the thread
Thread Create NewThread(0) To hThread
'Note that each thread will have it's own unique thread handle
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,220, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Start Thread", 50,10,100,20
Control Add Label, hDlg, 300,"", 10,70,80,20 : Control Add Label, hDlg, 305,"", 100,70,80,20 :
Control Add Label, hDlg, 301,"", 10,100,80,20 : Control Add Label, hDlg, 306,"", 100,100,80,20 :
Control Add Label, hDlg, 302,"", 10,130,80,20 : Control Add Label, hDlg, 307,"", 100,130,80,20 :
Control Add Label, hDlg, 303,"", 10,160,80,20 : Control Add Label, hDlg, 308,"", 100,160,80,20 :
Control Add Label, hDlg, 304,"", 10,190,80,20 : Control Add Label, hDlg, 309,"", 100,190,80,20 :
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local var&, i As Long
Dim hThread(9) As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
For i = 0 To 9
Thread Create NewThread(300+i) To hThread(i) 'start the new thread
Thread Close hThread(i) To hThread(i) 'suggested by PowerBASIC Inc. as good practice
Dialog Doevents
Sleep 0
Next i
End If
End Function
Thread Function NewThread (ByVal x As Long) As Long
Local iCount&
Do
Incr iCount& : Control Set Text hDlg, x, "Thread " + Format$(x-300,"#0") + ": " + Str$(iCount&)
Sleep 0
Loop Until iCount& > 2000
End Function
'gbs_00348
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm