Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Dec 17, 2017 6:38 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: question about loop behavior
PostPosted: Wed Mar 06, 2013 8:17 pm 
Offline

Joined: Wed Mar 06, 2013 7:48 pm
Posts: 4
Hello everyone. I am a new member of the forum. I have been programming for many years and a RB user since 2005. I am a hobbyist programming for my own amusement.
I have run into a problem with the following code.

// runs the specified number of days

dim i as integer // loop counter
dim days as integer // the number of days to run

// find number of days to run
days = val(NumOfDays.text)

// loop through that number of days
for i = 1 to days
RunOneday(animals(), TheWorld())

// show progress
if i mod 1000 = 0 then
App.DoEvents()
DaysRun.text = str(i)
end if
next

// show world
clr
DrawWorld(TheWorld())

This is the main loop from an evolution simulation. When running for a large number of days(several millions) it takes over an hour.
I coded a routing to update me on the progress every 1000 days. If run as above it works fine. However, if I comment out the DoEvents
line, the textarea is not updated until the routine finishes. Why is this ? Obviously the loop is keeping control. The documentation says not to use DoEvents in a single threaded application which this is. The code is working fine. I am just curious what is happening? Thanks in advance for any help that you can give me. Victor
I am using RS 2012 R2 on Mac osX 10.5.8


Top
 Profile  
Reply with quote  
 Post subject: Re: question about loop behavior
PostPosted: Thu Mar 07, 2013 12:37 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
I would recommend you put the code into a thread. As is, it will make your app appear unresponsive while it runs.


Top
 Profile  
Reply with quote  
 Post subject: Re: question about loop behavior
PostPosted: Thu Mar 07, 2013 3:35 pm 
Offline

Joined: Wed Mar 06, 2013 7:48 pm
Posts: 4
timhare wrote:
I would recommend you put the code into a thread. As is, it will make your app appear unresponsive while it runs.

Tim, Of course you are correct. However, I don't care about that for 2 reasons.
1. The only thing that the program runs is the simulation. Therefore, there is nothing else for the user to do while it runs.
2. I am the only user and don't care that it is unresponsive.
However, my question is why doesn't the textarea update while the loop is running unless I call DoEvents. I am trying to understand what is going on here. Perhaps the UI is locked while the loop is running? Thanks, Victor


Top
 Profile  
Reply with quote  
 Post subject: Re: question about loop behavior
PostPosted: Thu Mar 07, 2013 3:38 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
Do not use App.DoEvents. Instead, after you change your field, call Refresh:
DaysRun.Text= str( i )
DaysRun.Refresh()

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: question about loop behavior
PostPosted: Thu Mar 07, 2013 3:57 pm 
Offline

Joined: Mon Aug 14, 2006 9:33 pm
Posts: 1774
Quote:
Perhaps the UI is locked while the loop is running?

Your assumption is correct. Use Tim's thread idea of Kem's multiple refreshes to avoid the problem.

_________________
Roger Clary
Class One Software
Educational Software for Lifelong Learning
http://www.classonesoftware.com


Top
 Profile  
Reply with quote  
 Post subject: Re: question about loop behavior
PostPosted: Thu Mar 07, 2013 4:59 pm 
Offline

Joined: Wed Mar 06, 2013 7:48 pm
Posts: 4
ktekinay wrote:
Do not use App.DoEvents. Instead, after you change your field, call Refresh:
DaysRun.Text= str( i )
DaysRun.Refresh()


This works fine. Thanks everyone Victor


Top
 Profile  
Reply with quote  
 Post subject: Re: question about loop behavior
PostPosted: Fri Mar 08, 2013 1:09 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Remember, though, that you're not the only one watching your app. Using Refresh in the main thread, you still run the risk of the OS deciding your app is hung and helpfully offering to kill it for you. Especially if the event queue backs up.


Top
 Profile  
Reply with quote  
 Post subject: Re: question about loop behavior
PostPosted: Sat Mar 09, 2013 6:03 pm 
Offline

Joined: Wed Mar 06, 2013 7:48 pm
Posts: 4
timhare wrote:
Remember, though, that you're not the only one watching your app. Using Refresh in the main thread, you still run the risk of the OS deciding your app is hung and helpfully offering to kill it for you. Especially if the event queue backs up.

I will think about this. I am probably going to have to use a thread. Victor


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group