New topic: 

Using Timer to maintain the UI

<http://forums.realsoftware.com/viewtopic.php?t=45964>

         Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic          Author  
Message        CloudHugger          Post subject: Using Timer to maintain the 
UIPosted: Sat Nov 17, 2012 6:04 pm                         
Joined: Wed Nov 14, 2012 5:29 am
Posts: 5                Hi All,

Going in loops here - literally  - and cant maintain the UI. 

I understand vaguely the concepts of using a Timer rather than just expecting a 
window object value to be refreshed, but cannot make it happen. Any tips most 
appreciated!

Scenario is - I am executing a for..next loop from within the action method of 
a push button on my main window and within that for..next loop I am setting a 
Module property of string to a filename. This changes on each loop.

To indicate progress on the UI that something is happening, I'd like to update 
a text field on the main window with the string in the module.  It only needs 
to update each time it changes.

Am a RS newbie and tried to use other advice I've read here without success. 
Any pointers? I'd have thought this would be a simple common task but is 
proving very frustrating! 

Cheers
CH   
                             Top                DaveS          Post subject: 
Re: Using Timer to maintain the UIPosted: Sat Nov 17, 2012 6:09 pm              
                   
Joined: Sun Aug 05, 2007 10:46 am
Posts: 4252
Location: San Diego, CA                something this perhaps in you MODULE


x=x+1
window1.text1.text=str(x)
window1.text1.invalidate
      
_________________
Dave Sisemore
MacPro, OSX Lion 10.7.4 RB2012r1
Note : I am not  interested in any solutions that involve custom Plug-ins of 
any kind  
                             Top                basestring          Post 
subject: Re: Using Timer to maintain the UIPosted: Sat Nov 17, 2012 6:39 pm     
                            
Joined: Sat May 28, 2011 11:28 pm
Posts: 121
Location: Beijing China                try to put you loop stuff in a thread
then when you press the button, then use something like thread1.run

this keeps your UI active, so will be easy to update your UI and your loop will 
be much faster

then no need for timers!!!!      
_________________
For great Music got to my podcast Website!!!
http://podcast.1945mf-china.com  
                             Top                charonn0          Post subject: 
Re: Using Timer to maintain the UIPosted: Sat Nov 17, 2012 6:43 pm              
                   
Joined: Mon Apr 02, 2007 2:08 am
Posts: 1013
Location: San Francisco, CA, USA                The timer and the pushbutton 
both have their Action events which are called by the main event loop. If you 
have a long-running loop inside an event handler, then other events (like 
Timer.Action) don't fire until after the loop has finished. basestring is 
right, use a thread in this case.      
_________________
Boredom Software  
                             Top                doofus          Post subject: 
Re: Using Timer to maintain the UIPosted: Sat Nov 17, 2012 7:41 pm              
                   
Joined: Thu Sep 10, 2009 2:50 am
Posts: 301
Location: Santa Cruz, CA, USA                To update the UI in a tight loop 
call Refresh on your control after updating it.

In this example I'm setting the name directly instead of going through a 
module...

for i As integer = 0 to myFiles.Ubound
  
  progressLabel.Text = "processing " + myFiles(i).Name
  progressLabel.Refresh  //redraws control immediately
  
  //processs myFile(i)
  
next

progressLabel.Text = "done processing"



Running in a tight loop like this your app will be locked up, but the feedback 
will be flowing. I usually add a way to short-circuit the loop, like holding 
down a couple keys...

if Keyboard.AsyncShiftKey and Keyboard.AsyncControlKey then exit for i
This is non-standard of course, for quick and dirty development.


To use a Timer have the pushbutton build a list of what to process then start 
the timer where it does the actual processing.

before
//pushbuttons Action
for i As integer = 0 to myFiles.Ubound
  //process myFiles(i)
next


refactored
//pushbuttons Action
redim toProcessInTimer(-1)  //reset list to process
for i As integer = 0 to myFiles.Ubound  //build list
  toProcessInTimer.Append myFiles(i)
next
theTimer.Mode = 2 //start timer

//Timers Action
if toProcessInTimer.Ubound < 0 then //no items left, stop
  me.Mode = 0
  progressLabel.Text = "done"
else            //get next item and process
  dim f As FolderItem = toProcessInTimer.Pop
  progressLabel.Text = "processing " + f.Name
  //process f
end



If processing a single file is time consuming your app will still lockup in the 
timer, only responding between firings of the timer. In that case use a thread. 
  
                             Top                timhare          Post subject: 
Re: Using Timer to maintain the UIPosted: Sun Nov 18, 2012 2:25 am              
           
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 11816
Location: Portland, OR  USA                The pertinent point:
doofus wrote:This is non-standard of course, for quick and dirty development.
For production work, use a thread.  Do not set the UI directly from a thread.  
Instead set a property of the window/app/module and use a timer to set the UI 
based on that property.   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to