Date: 02-16-2022
Return to Index
created by gbSnippets
'When a thread is created or when a thread closes, then is no
'Windows-generated message to acknowledge the event.
'However, a user-defined message can be created to indicate the
'start and end of a thread.
'Primary Code:
'Define a message #
%CM_THREAD_COMPLETE = %WM_USER + 1
%CM_THREAD_COMPLETE = %WM_USER + 2
'Place code in the dialog Callback to capture the message
Case %CM_THREAD_START
Case %CM_THREAD_COMPLETE
Thread Close hThread TO hThread
'Post the messages
Function PrimeThread(ByVal hDlg As Long) As Long
Dialog Post hDlg, %CM_THREAD_START, 0, 0
Dialog Post hDlg, %CM_THREAD_COMPLETE, 0, 0
End Function
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%CM_THREAD_START = %WM_USER + 1
%CM_THREAD_COMPLETE = %WM_USER + 2
Global hDlg as Dword, hThread as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Start Thread", 50,10,100,20
Control Add Label, hDlg, 300,"<extra thread count>", 50,70,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_Command
If CB.Ctl = 100 Then Thread Create MyThread(0) To hThread 'new thread started
Case %CM_THREAD_START
MsgBox "Thread Started"
Case %CM_THREAD_COMPLETE
Thread Close hThread TO hThread
MsgBox "Thread Ended"
End Select
End Function
Thread Function MyThread (ByVal x As Long) As Long
Dialog Post hDlg, %CM_THREAD_START, 0, 0 'thread-started message
Static iCount&
Sleep 500 'not required - just for visual offset
Do
Incr iCount& : Control Set Text hDlg, 300, "Thread" + Str$(iCount&)
Dialog Doevents
Loop Until iCount& > 2000
iCount& = -1 'so thread can be restarted
Dialog Post hDlg, %CM_THREAD_COMPLETE, 0, 0 'thread-over message
End Function
'gbs_00349
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm