Date: 02-16-2022
Return to Index
created by gbSnippets
'A thread ends when the Thread Function ends. Or, since thread functions have
'access to Global variables, you can use a Global variable to stop a thread.
'Primary Code:
'This Global variable:
Global PleaseStop&
'And this response to a user pressing a button, is all that is needed to stop a thread.
If CB.Ctl = 150 Then PleaseStop& = %True
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as DWord, hThread as DWord, PleaseStop&
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 Button, hDlg, 150,"Please Stop", 50,40,100,20
Control Add Label, hDlg, 300,"<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
Thread Close hThread To hThread 'suggested by PowerBASIC Inc. as good practice
PleaseStop& = %False
End If
If CB.Ctl = 150 Then PleaseStop& = %True
End Select
End Function
Thread Function MyThread (ByVal x As Long) As Long
Static iCount&
Do
Incr iCount& : Control Set Text hDlg, 300, "Thread" + Str$(iCount&)
Dialog Doevents
Loop Until iCount& > 2000 Or PleaseStop& = %True
iCount& = -1 'so button can restart the thread again
End Function
'gbs_00347
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm