Re: ClearGadgetItems() doesn't clear first row in EditorGadg
Posted: Mon Apr 04, 2016 2:23 pm
if you want to bypass the problem completely, you should outsource the timeconsumingcode in an extra thread.
http://www.purebasic.com
https://www.purebasic.fr/english/
What I did for my own app LumaTone, is to let the time consuming thread allocate some new memory for every text message using AllocateMemory, fill it with the text message and post en event using PostEvent with the pointer that AllocateMemory returned as data.collectordave wrote:The thread solution posted earlier but it does not allow lord to set the text from the timeconsuming code as it is in a thread.
Code: Select all
Procedure SetSomeText()
Static x
ClearGadgetItems(2)
UpdateWindow_(GadgetID(2))
x=1-x
If x
SetGadgetText(2, "1111111111111111111"+#CRLF$+"1111111111111111111")
Delay(500)
Else
SetGadgetText(2, "2222222222222222222"+#CRLF$+"2222222222222222222")
Delay(500)
EndIf
EndProcedure
OpenWindow(1, 10, 10, 640, 480, "")
StringGadget(1, 55, 5, 580, 20, "")
EditorGadget(2, 5, 30, 630, 445, #PB_Editor_WordWrap)
AddKeyboardShortcut(1, #PB_Shortcut_Return, 10)
BindEvent(#PB_Event_Menu, @SetSomeText(), 1)
Quit=#False
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit=#True
EndSelect
Until Quit=#True
Code: Select all
Procedure SetSomeText()
Static x
FreeGadget(2)
EditorGadget(2, 5, 30, 630, 445, #PB_Editor_WordWrap)
x=1-x
If x
SetGadgetText(2, "1111111111111111111"+#CRLF$+"1111111111111111111")
Delay(500)
Else
SetGadgetText(2, "2222222222222222222"+#CRLF$+"2222222222222222222")
Delay(500)
EndIf
EndProcedure
OpenWindow(1, 10, 10, 640, 480, "")
StringGadget(1, 55, 5, 580, 20, "")
EditorGadget(2, 5, 30, 630, 445, #PB_Editor_WordWrap)
AddKeyboardShortcut(1, #PB_Shortcut_Return, 10)
BindEvent(#PB_Event_Menu, @SetSomeText(), 1)
Quit=#False
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit=#True
EndSelect
Until Quit=#True
All things on the GUI stop when running code that does not process the event loop at least occasionally.It is a windows program, which is event driven.
A call to something which modifies the GUI is not handled immediately.
At least one round through the event loop is needed.
Code: Select all
Global Text.s
Procedure SetSomeText()
Static x
Select x
Case 0
ClearGadgetItems(2)
Text = ""
Case 1
For i=1 To 25
Text = Text +"11111111111111111111111111111111"+#CRLF$
Next
SetGadgetText(2, Text)
Text = ""
Case 2
For i=1 To 25
Text = Text +"22222222222222222222222222222222"+#CRLF$
Next
SetGadgetText(2, Text)
text = ""
EndSelect
x = x + 1
If x > 2
x = 0
EndIf
EndProcedure
Procedure TimeConsumingCode()
For i=1 To 5000000
;Some time consuming Code
StartTime = ElapsedMilliseconds()
While endtime < 5000
EndTime = ElapsedMilliseconds()-StartTime
Wend
Select i
Case 1000000
text = "Just testing after first 1000000"
SetGadgetText(2, Text)
;UpdateWindow_(GadgetID(2))
While WindowEvent() : Wend
;More time consuming Code
StartTime = ElapsedMilliseconds()
While endtime < 5000
EndTime = ElapsedMilliseconds()-StartTime
Wend
Case 2000000
SetSomeText()
;UpdateWindow_(GadgetID(2))
While WindowEvent() : Wend
;More time consuming Code
StartTime = ElapsedMilliseconds()
While endtime < 5000
EndTime = ElapsedMilliseconds()-StartTime
Wend
Case 3000000
SetSomeText()
While WindowEvent() : Wend
;UpdateWindow_(GadgetID(2))
;More time consuming Code
StartTime = ElapsedMilliseconds()
While endtime < 5000
EndTime = ElapsedMilliseconds()-StartTime
Wend
Case 4000000
SetSomeText()
;More time consuming Code
StartTime = ElapsedMilliseconds()
While endtime < 5000
EndTime = ElapsedMilliseconds()-StartTime
Wend
While WindowEvent() : Wend
;UpdateWindow_(GadgetID(2))
EndSelect
While WindowEvent() : Wend
;More time consuming Code
StartTime = ElapsedMilliseconds()
While endtime < 5000
EndTime = ElapsedMilliseconds()-StartTime
Wend
y=Pow(2, i)
Next
Debug "Time Consuming Code Finished"
EndProcedure
OpenWindow(1, 10, 10, 640, 480, "")
ButtonGadget(1, 5, 5, 100, 20, "")
EditorGadget(2, 5, 30, 630, 445, #PB_Editor_WordWrap)
AddKeyboardShortcut(1, #PB_Shortcut_Return, 10)
BindEvent(#PB_Event_Menu, @SetSomeText(), 1)
Quit=#False
Text = ""
TimeConsumingCode()
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit=#True
Case #PB_Event_Gadget
Select EventGadget()
Case 1
Debug "Time Consuming Code Started"
TimeConsumingCode()
EndSelect
EndSelect
Until Quit=#True
Code: Select all
UpdateWindow_(GadgetID(2))
Code: Select all
Procedure SetSomeText()
Static x
r = CountGadgetItems(2)
For row = r To 0 Step -1
RemoveGadgetItem(2,row)
Next
x=1-x
If x
SetGadgetText(2, "1111111111111111111"+#CRLF$+"1111111111111111111")
Delay(500)
Else
SetGadgetText(2, "2222222222222222222"+#CRLF$+"2222222222222222222")
Delay(500)
EndIf
EndProcedure
OpenWindow(1, 10, 10, 640, 480, "")
StringGadget(1, 55, 5, 580, 20, "")
EditorGadget(2, 5, 30, 630, 445, #PB_Editor_WordWrap)
AddKeyboardShortcut(1, #PB_Shortcut_Return, 10)
BindEvent(#PB_Event_Menu, @SetSomeText(), 1)
Quit=#False
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit=#True
EndSelect
Until Quit=#True
Code: Select all
Procedure SetSomeText()
Static x
r = CountGadgetItems(2)
RemoveGadgetItem(2,r)
ClearGadgetItems(2)
x=1-x
If x
SetGadgetText(2, "1111111111111111111"+#CRLF$+"1111111111111111111")
Delay(500)
Else
SetGadgetText(2, "2222222222222222222"+#CRLF$+"2222222222222222222")
Delay(500)
EndIf
EndProcedure
OpenWindow(1, 10, 10, 640, 480, "")
StringGadget(1, 55, 5, 580, 20, "")
EditorGadget(2, 5, 30, 630, 445, #PB_Editor_WordWrap)
AddKeyboardShortcut(1, #PB_Shortcut_Return, 10)
BindEvent(#PB_Event_Menu, @SetSomeText(), 1)
Quit=#False
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit=#True
EndSelect
Until Quit=#True