Page 1 of 1

menu & combo block update

Posted: Tue Mar 30, 2021 5:08 am
by collectordave
I need to update a progress text while my media player app is running.

Used addwindowtimer() set for 1 second.

worked ok.

Now adding more code for the user to make other choices while a song is playing.

Whenever a menu is opened (no choice made) or a combobox is opened (No choice made) the update stops.

I have tried running the update in a thread but get strange memory errors when running, these errors are random and happen in the code anywhere depending on what the user is doing.

I have read that updating a gadget on the main form from a thread is not good practice and when I do not use the thread the errors do not happen.

Below a little code to illustrate the problem.

Code: Select all

Procedure Update()
  
  Static iLoop
  
  iLoop = iLoop + 1
  
  SetGadgetText(2,Str(iLooP) + " Seconds")

 EndProcedure
 

 OpenWindow(0,5,5,400,200,"Test Window")
 
 ComboBoxGadget(1,5,5,80,20)
 
 TextGadget(2,90,5,80,20,"Seconds")
 
 For i = 0 To 9
   AddGadgetItem(1,-1,"Item " + Str(i))
 Next
 
 AddWindowTimer(0,100,1000)
 
 
 Repeat
     Event = WaitWindowEvent()
     
     If Event = #PB_Event_Timer
       
       UpDate()
       
     EndIf

   Until Event = #PB_Event_CloseWindow
 
Does anyone know how to keep the update running when a menu or combo is selected?

Regards

CD

Re: menu & combo block update

Posted: Tue Mar 30, 2021 5:25 am
by RASHAD
Maybe

Code: Select all

Procedure Update()
 
  Static iLoop
 
  iLoop = iLoop + 1
 
  SetGadgetText(2,Str(iLooP) + " Seconds")

 EndProcedure
 

 OpenWindow(0,5,5,400,200,"Test Window")
 
 ComboBoxGadget(1,5,5,80,20)
 
 TextGadget(2,90,5,80,20,"Seconds")
 
 For i = 0 To 9
   AddGadgetItem(1,-1,"Item " + Str(i))
 Next
 
 AddWindowTimer(0,100,1000)
 BindEvent(#PB_Event_Timer,@Update())
 
 Repeat
     Event = WaitWindowEvent()
     
     ;If Event = #PB_Event_Timer
       
       ;UpDate()
       
     ;EndIf

   Until Event = #PB_Event_CloseWindow

Re: menu & combo block update

Posted: Tue Mar 30, 2021 5:30 am
by collectordave
Thanks Rashad but the update is still blocked it seems the combobox when the list is down blocks the main event loop.

Regards

CD

Re: menu & combo block update

Posted: Tue Mar 30, 2021 5:33 am
by RASHAD
Tested with PB 5.73 x86 - Windows 10 x64
Work fine as is should be
What is your configuration ?

Re: menu & combo block update

Posted: Tue Mar 30, 2021 5:57 am
by collectordave
PB 5.73 LTS

MAC OS 10.14.6


Just a problem for the MAC?

Re: menu & combo block update

Posted: Tue Mar 30, 2021 6:11 am
by collectordave
This appears to work:

Code: Select all

Enumeration #PB_Event_FirstCustomValue
    #EventUpDate
EndEnumeration

Global Quit.i

Procedure Update()
 
  Static iLoop
 
  iLoop = iLoop + 1
 
  SetGadgetText(2,Str(iLooP) + " Seconds")

 EndProcedure
 
 Procedure MyTimer(*Val)
   
   Static lt.i
   
   While Quit = #False
       If lt.i < ElapsedMilliseconds()

       UpDate()
       lt = ElapsedMilliseconds() + 1000
       
     EndIf 
  Wend 
   
 EndProcedure
 
 

 OpenWindow(0,5,5,400,200,"Test Window")
 
 ComboBoxGadget(1,5,5,80,20)
 
 TextGadget(2,90,5,80,20,"Seconds")
 
 For i = 0 To 9
   AddGadgetItem(1,-1,"Item " + Str(i))
 Next

 Thread = CreateThread(@MyTimer(),#NUL)

 Repeat
   Event = WaitWindowEvent(1)

   If Event = #PB_Event_CloseWindow
     Quit = #True
   EndIf

 Until Event = #PB_Event_CloseWindow
But it is updating the gadget from the thread.

Is this Ok? Maybe if I create Theadsafe app but I do not understand what Threadsafe does.

CD

Re: menu & combo block update

Posted: Tue Mar 30, 2021 6:47 am
by infratec
ThreadSave stores and restores additional registers.

In general:
It is a bad idea to access GUI elements inside of a thread.

Post an Event to the main loop and let the main loop do the GUI stuff.

Re: menu & combo block update

Posted: Tue Mar 30, 2021 7:22 am
by collectordave
infratec wrote:ThreadSave stores and restores additional registers.

In general:
It is a bad idea to access GUI elements inside of a thread.

Post an Event to the main loop and let the main loop do the GUI stuff.
Hi

I have tried posting an event to the main loop but it seems the main loop is hung up and never sees the event when the combo box or a menu is active.

I also tried posting an event and used Bindevent() to attempt to capture that event and that did not work either.

I started to look at this after attempting to update a list gadget from a thread which just did not work and sometimes created havoc.

The logic in the app allowed me to post an event when this list needed updating and it works fine.

Maybe I need to ensure that the text gadget is always updated with a string of the same length?

I have added my code above to the main app and it seems to work fine so far no errors.

Re: menu & combo block update

Posted: Tue Mar 30, 2021 3:15 pm
by netmaestro
My comment: What infratec said. Stay with it until it works for you. It is an approach known to work and tested by time.

Re: menu & combo block update

Posted: Tue Mar 30, 2021 7:08 pm
by mk-soft
For macOS you need a RunLoopTimer ...

Link: viewtopic.php?f=19&t=50795&start=195#p565723

Re: menu & combo block update

Posted: Wed Mar 31, 2021 5:41 am
by collectordave
Thanks mk.soft

That is exactly what I was looking for. Even down to the notes after the article.

I did not know about the resize blocking the loop as well.

Thanks again.