why a loop eat ram ??

Hi, in my web app i have create a webDialog to show … “Wait, the file must be created and then you can download this…”
i have an other external app that creates these files ex… file1.ppp, file2.ppp, file3.ppp…
My webDialog must shows “wait …the file must be created”, when the file is completely created
(and external procedure has completed his steps… and the file like file1.ppp is closed and not locked)
my webDialog must shows a button to download this file…

i have created WebDialogW with a webTimer with ModeSingle.
this is the code inside timer :

dim f as folderitem
dim f1 as folderitem
dim fn as string
dim fn1 as string

fn=“C:\file1.ppp”
fn1=fn+"_tmp"
f=getfolderitem(fn, …absolutepath…)
do until f.exist
f=getfolderitem("fn, …absolutepath…)
loop

'now the file exist and to know if it is in exclusive mode (if other application not closes the file, the file result locked) i try to rename this… and catch error…

f1=getfolderitem(fn1, …)
do until f1.exist
try
f.name=fn1
catch

end try
f1=getfolderitem(fn1,…)
loop
'…now the f1 exist and the f is not in exclusive mode
f.name=fn

now set webTimer to ModeOff and activate download button.

all work fine… but if i watch task scheduler , when my webApp uses 15mb of ram, at the end of these
loops uses 65mb of ram…
can anyone help me ???
sorry for my bad english…

I can’t comment on memory usage, but these lines

do until f.exist
    f=getfolderitem("fn, ..absolutepath..)
loop

… are going to use 100% CPU for no reason - do you really need to check a million times per second if the file is there? Probably not.
Put a sleep() call in to make the app behave nicer:

[code]
do until f.exist
f=getfolderitem("fn, …absolutepath…)
app.sleepCurrentThread(10) // this will sleep for 10 milliseconds
loop

tnx Michael,
i don’t know this function, and from help i don’t understand if this
stop procedure where it is only, or stop the webApp
if stop webApp i have problems… because other users must work without stops…

and i can’t find --> app.sleepCurrentThread

https://documentation.xojo.com/index.php/Application.SleepCurrentThread

You guys are almost finding a bad design “feature”. :slight_smile:

https://documentation.xojo.com/index.php/Application.SleepCurrentThread
i don’t understand if it is usable to WEBapp… but i think no

  • You’re creating a ton of FolderItem objects and Xojo may not be releasing them until after the method exits.

  • I don’t know if it’s a good idea to sleep a web event handler thread. The client side framework is expecting a response, and I generally design my web code to complete events as quickly as possible. (I spin long tasks off into threads or timers, whichever is appropriate.) Get rid of your loops and use the WebTimer in multiple mode. That way you can control how often the file is checked via WebTimer.Period, and you give Xojo a chance to dispose the file objects you create.

  • I would try something other then a rename to see if the file was in use. Since Xojo doesn’t seem to expose this information in its framework (FolderItem.Locked doesn’t cover this), I would probably look up the Win32 API for checking to see if a file is in use.

  • Even with these changes, this is a less then ideal design because it’s going to generate unnecessary disk I/O on a server. If you are also writing the external app you can modify it to signal the WE app once the file is ready. If you can’t do anything about the external app and/or the impact will be low it may be perfectly acceptable. But only if you manage your file check frequency with a WebTimer. Otherwise you will drive the CPU to 100%, thrash the HD, and slow down the external app if it’s running on the same server.