I simplified the codes from @Axolotl
my article https://erolcum-blogspot-com.translate ... r_pto=wapp
// Moved from "Tricks und Tipps" to "Coding Questions" (Kiffi)
Code: Select all
Case #PB_Event_CloseWindow
ApplicationQuit = #True
End
Code: Select all
EnableExplicit
CompilerIf Not #PB_Compiler_Thread
CompilerError "Enable thread safe in compiler options!"
CompilerEndIf
#WINDOW_Main = 0
#WINDOW_Main_Flags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
#LOOP_MaxLoops = 200
#LOOP_SlowDownDelay = 100
Enumeration Gadget
#GADGET_btnStart
#GADGET_btnStop
#GADGET_chkTest
#GADGET_txtLoopOutput
#GADGET_lbTrace
EndEnumeration
Enumeration OwnEvent #PB_Event_FirstCustomValue
#OwnEvent_ThreadStarted
#OwnEvent_SetGadgetText
#OwnEvent_ThreadFinished
EndEnumeration
Structure Parameter_Structure
Thread.i
Exit.i
EndStructure
Macro Trace(MessageText)
AddGadgetItem(#GADGET_lbTrace, -1, MessageText)
SetGadgetState(#GADGET_lbTrace, CountGadgetItems(#GADGET_lbTrace) -1)
EndMacro
Procedure Test_LongLoop(*Parameter.Parameter_Structure)
Protected index.i, *msg
PostEvent(#OwnEvent_ThreadStarted)
For index = 0 To #LOOP_MaxLoops
If *Parameter\Exit
Break
EndIf
Delay(#LOOP_SlowDownDelay) ; spend some time with nothing
*msg = UTF8(" Item " + Str(index) + "/" + Str(#LOOP_MaxLoops) + " | Checkbox.State == " + GetGadgetState(#GADGET_chkTest))
PostEvent(#OwnEvent_SetGadgetText, #WINDOW_Main, #GADGET_txtLoopOutput, 0, *msg)
Next index
*msg = UTF8("Loop finished (reached MaxLoops == " + Str(index) + ")")
PostEvent(#OwnEvent_ThreadFinished, #WINDOW_Main, #GADGET_lbTrace, 0, *msg)
EndProcedure
Define.i Event, ApplicationQuit, Exit
Define *msg
Define Parameter.Parameter_Structure
OpenWindow(#WINDOW_Main, 0, 0, 400, 320, "Event handling example...", #WINDOW_Main_Flags)
StickyWindow(#WINDOW_Main, 1) ; show test app always above the PB_IDE
ButtonGadget(#GADGET_btnStart, 8, 4, 192, 32, "Start Looping")
ButtonGadget(#GADGET_btnStop, 200, 4, 192, 32, "Stop Looping")
DisableGadget(#GADGET_btnStop, #True)
CheckBoxGadget(#GADGET_chkTest, 8, 40, 384, 24, "Check me -- (see the event handling while the loop is running)")
TextGadget(#GADGET_txtLoopOutput, 8, 72, 384, 20, "Loop is stopped.")
SetGadgetColor(#GADGET_txtLoopOutput, #PB_Gadget_BackColor, GetSysColor_(#COLOR_INFOBK))
ListViewGadget(#GADGET_lbTrace, 8, 104, 384, 208, $4000) ; #LBS_NOSEL == 0x4000
Repeat
Event = WaitWindowEvent()
Select Event
Case #OwnEvent_ThreadStarted
DisableGadget(#GADGET_btnStart, #True)
DisableGadget(#GADGET_btnStop, #False)
SetGadgetText(#GADGET_txtLoopOutput, "Loop is running")
Case #OwnEvent_SetGadgetText
*msg = EventData()
SetGadgetText(EventGadget(), PeekS(*msg, -1, #PB_UTF8))
FreeMemory(*msg)
Case #OwnEvent_ThreadFinished
DisableGadget(#GADGET_btnStart, #False)
DisableGadget(#GADGET_btnStop, #True)
*msg = EventData()
If *msg
Trace(PeekS(*msg, -1, #PB_UTF8))
FreeMemory(*msg)
EndIf
SetGadgetText(#GADGET_txtLoopOutput, "Loop stopped.")
If ApplicationQuit
Exit = #True
EndIf
Case #PB_Event_MoveWindow
Trace(" New Window Pos == " + WindowX(#WINDOW_Main) + ", " +WindowY(#WINDOW_Main))
Case #PB_Event_CloseWindow
ApplicationQuit = #True
If IsThread(Parameter\Thread)
Parameter\Exit = #True
Else
PostEvent(#OwnEvent_ThreadFinished)
EndIf
Case #PB_Event_Gadget
Select EventGadget()
Case #GADGET_btnStart
ClearGadgetItems(#GADGET_lbTrace)
Parameter\Exit = #False
Parameter\Thread = CreateThread(@Test_LongLoop(), @Parameter)
Case #GADGET_btnStop
If IsThread(Parameter\Thread)
Parameter\Exit = #True
EndIf
Case #GADGET_chkTest
Trace("Gadget = #GADGET_chkTest == " + GetGadgetState(#GADGET_chkTest))
EndSelect
EndSelect
Until Exit
Code: Select all
GetSysColor_(#COLOR_INFOBK)