menu & combo block update

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

menu & combo block update

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4136
Joined: Sun Apr 12, 2009 6:27 am

Re: menu & combo block update

Post 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
Egypt my love
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: menu & combo block update

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4136
Joined: Sun Apr 12, 2009 6:27 am

Re: menu & combo block update

Post by RASHAD »

Tested with PB 5.73 x86 - Windows 10 x64
Work fine as is should be
What is your configuration ?
Egypt my love
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: menu & combo block update

Post by collectordave »

PB 5.73 LTS

MAC OS 10.14.6


Just a problem for the MAC?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: menu & combo block update

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
infratec
Always Here
Always Here
Posts: 5570
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: menu & combo block update

Post 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.
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: menu & combo block update

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8215
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: menu & combo block update

Post 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.
BERESHEIT
User avatar
mk-soft
Addict
Addict
Posts: 3294
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: menu & combo block update

Post by mk-soft »

For macOS you need a RunLoopTimer ...

Link: viewtopic.php?f=19&t=50795&start=195#p565723
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: menu & combo block update

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply