[Gambas-user] App idle with long loops

Tobias Boege taboege at ...626...
Sat Oct 8 18:09:34 CEST 2016


On Sat, 08 Oct 2016, Demosthenes Koptsis wrote:
> Hello,
> 
> i have very long (time consuming) For loops and my app is frozen until 
> loop is finished.
> 
> for example i wget urls with a custom Sub which Shell("wgetURLS.sh") To 
> sOUTPUT
> 
>    'get urls
>    For i = 0 To iDepth
>      wgetURLS(i * 10)
>    Next
> 
> Public Sub wgetURLS(iStart As Integer)
> .....
> 
> Shell("wgetURLS.sh") To sOUTPUT
> 
> .....
> 
> --------------
> 
> Is there possible to set the gui idle and the same time run For...loops?
> 

If you only want to refresh the GUI (e.g. if your For loops update a
ProgressBar on your form and you want to update the value of that bar
during the loop), then Wait [1] is sufficient.

If you want the GUI to be fully operatable while your loops run in the
background, you have to use a background Task [2]. As Gambas is single-
threaded, Tasks are implemented as external processes which execute one
of your classes. This limits the things you can do with Tasks (you cannot
directly modify the main program, for example, but have to send data and
status reports through a pipe from your Task to the main process and
interpret them there). However, if all you want is loading some files via
wget, that should not be a problem.

Of course, there is also the gb.net.curl component which can download files
asynchronously. If you can use that (which depends on what you want to do),
you should.

And as a fourth option, looking further into your code: if you want to
execute shell scripts in a For loop, then don't use the Shell-To syntax
but create a Process object instead and accumulate its output in Read
events. The event loop will take care of everything and the GUI will be
usable without any extra effort on your side.

Finding the best solution depends, who would have thought, on what you want
to do *specifically*. Tasks are the most general and most cumbersome option.

Regards,
Tobi

[1] http://gambaswiki.org/wiki/lang/wait
[2] http://gambaswiki.org/wiki/comp/gb/task

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list