Another GetDirectorySize() Procedure (PB4.01)

Share your advanced PureBasic knowledge/code with the community.
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post by techjunkie »

GeoTrail wrote:
thefool wrote:Ha! I win 8)

Code: Select all

For i=0 To 1000000
	CreateFile(0, "C:\Crap\CrapFile"+Str( i )+".txt")
	WriteStringN(0, "This is just a crap file...")
	WriteStringN(0, "This is just a crap file...")
	WriteStringN(0, "This is just a crap file...")
	WriteStringN(0, "This is just a crap file...")
	WriteStringN(0, "This is just a crap file...")
	WriteStringN(0, "This is just a crap file...")
	WriteStringN(0, "This is just a crap file...")
	CloseFile(0)
Next i
I win hehehe
Hehe... Doesn't work - the Crapsfiles stops generating at "CrapFile433464.txt". Windows limitation? Can you only have 433464 files in a directory?!?! :wink:
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Worked here, took me 37 minutes to do it.
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post by techjunkie »

GeoTrail wrote:Worked here, took me 37 minutes to do it.
Yeah - maybe I should have given it more time, but damn GeoTrail :wink: , it took forever to delete. Hanged my Laptop completely! :lol:
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

AND51 wrote:Thank you! Very interesting!
So if I createa threaad, the thread can start faster, if I've got a Delay(0) in my main module? And if there is no thread is to be started, my main module wouldn't be influcened, because Delay(0) immediately returns...
Pretty much yeah!
But even if you do not use multiple threads, it is still advised to do it in loops you know are "heavy" (use a lot of cpu, or takes a while to complete)

The machine will seem to work smoother. (users will be able to browse the net or watch a movie while they wait for the program to complete whatever it's doing)

Best of all, using Delay(0) in loops (while/for/foreach and similar) while developing a program is smart too,
if the loop is buggy you will still be able to bring up Task Manager or Kill it from the IDE.

So I recommend using Delay(0) in any application at the least,
preferably Delay(1) in the main window loop,
for "paused" programs I tend to use Delay(1000) (actualy WaitWindowEvent(1000) is what I use then instead)
I would not recommend using more than Delay(20) in a GUI loop if you use WindowEvent(), longer delays than 20ms makes the GUI very "sluggish".

Anyway, I'm getting totally off topic from the original posts here, sorry. :)
Maybe WaitWindowEvent(1000) and Delay(0) and Delay(1) should be added to the PB manual's Window and loop sections as "tips",
or add it to the FAQ or somewhere. veterans are aware of this (I hope) as it's considered good coding/multitask friendly coding and will benefit single cpu's as well as multi-cpu systems.

Beginner coders are not always aware of easy performance "tricks" like this. Remember, a program runs under a operating system so you must not consider only the performance of your program, but how your program affect the performance of the OS while running as well.

(PS! if anyone want to, feel free to "steal" my blabber above/rewrite it and post it as a Tips'n'tricks post or similar.)
thefool
Always Here
Always Here
Posts: 5881
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

GeoTrail wrote:Worked here, took me 37 minutes to do it.
Damn :!:
But I still win in directories :twisted:
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Ok, I'm gonna give that to you. Creating all those files took so damn long, don't wanna do that again :lol:
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

@ Resactor: Thank you very much for your information!!! :wink: :D
Can I also use WaitWindowEvent(0) insted of Delay(0)? Then I don't have to write so much.

Your knowlege is very useful. If you want, you can give me/us more of these information. :)
PB 4.30

Code: Select all

onErrorGoto(?Fred)
thefool
Always Here
Always Here
Posts: 5881
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

GeoTrail wrote:Ok, I'm gonna give that to you. Creating all those files took so damn long, don't wanna do that again :lol:
:lol:
Ok i gotta go defragmenting. cya in 837 hours :P

nah its quite fast actually; i use DiskKeeper
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

AND51 wrote:Can I also use WaitWindowEvent(0) insted of Delay(0)? Then I don't have to write so much.
Yes! It seems that WaitWindowEvent(0) works. However for a normal application I'd advise WaitWindowEvent() or WaitWindowEvent(1000) for example, and for apps that need to update it's display regulary use WaitWindowEvent(1) or WaitWindowEvent(20) depending on your needs,
as WaitWindowEvent(0) uses all free cpu. (but does not lock up the system luckily :)

Thanks to WaitWindowEvent() supporting a delay, I find WindowEvent() almost obsolete. (but just almost, as some games may need a custom delay loop for example by using WindowEvent() and Delay() in it's main loop)

Bah, right now I wish I was a mod so I could split these delay posts out of this thread and into general instead. This topic is like two different things now. *beats himself up*
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Sorry, I can't see any use for WaitWindowEvent(1000)? :roll:
And there is really no reason to use a Delay() with WaitWindowEvent().
Athlon64 3700+, 1024MB Ram, Radeon X1600
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

WindowEvent() ist not depreciated, as you sometimes need to empty the message queue in loops, for example:

Code: Select all

Foreach myList()
     While WindowEvent() : Wend

; CODE

next
Not emptying the message queue leads to a not reacting GUI, which is about to be destroyed and then it won't be redrawn.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

remi_meier wrote:Sorry, I can't see any use for WaitWindowEvent(1000)? :roll:
Sure there is.
WaitWindowEvent(1000) would return whenever there is a window event. But by specifying 1000 you would be guaranteed it would return aprox. once per second.

So you could use ElapsedMilliseconds() to meassure time passed, allowing you do to "automatic updates" checking in a very system friendly way.

WaitWindowEvent(1000) would also be usefull for a clock or anything else that need to be updated at least once per second.

Obviously you could use WaitWindowEvent(1000*3600) which is 1 hour or even WaitWindowEvent(1000*86400) which is 24 hours.

WaitWindowEvent() would still return instantly if any window events happen. (user clicking a button, system message etc)

Think of it as a forced event 0.
remi_meier wrote:And there is really no reason to use a Delay() with WaitWindowEvent().
*looks at the post above* Can't see I ever mentioned those two together at all. And no I do not recommend using WaitWindowEvent() and Delay() together.
AND51 wrote:WindowEvent() ist not depreciated
I said "I find WindowEvent() almost obsolete", as I do still use it in a few special cases, and I never said deprecated.


I'd rather prefer people quoted me exactly (with all the flaws, if any), rather than paraphrasing or reading something that was never written, thus totally changing the meaning of what I said.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

I meant, that it is even not almost depreciated/obsolete.

Question:

I showed you how I empty the message queue in heavy loops: While WindowEvent() : Wend

Can I also use While WaitWindowEvent(0) : Wend to have your Delay(0) effect in my loop? Then I don't have to write While WindowEvent() : Delay(0) : Wend.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

>> And there is really no reason to use a Delay() with WaitWindowEvent().

*looks at the post above* Can't see I ever mentioned those two together at all. And no I do not recommend using WaitWindowEvent() and Delay() together.
Actually, this passage could lead to misunderstandings:
Rescator wrote:So I recommend using Delay(0) in any application at the least,
preferably Delay(1) in the main window loop,
for "paused" programs I tend to use Delay(1000) (actualy WaitWindowEvent(1000) is what I use then instead)
I would not recommend using more than Delay(20) in a GUI loop if you use WindowEvent(), longer delays than 20ms makes the GUI very "sluggish".
For WaitWindowEvent(1000):
Actually, I personally would use a timer/callback or a thread for something
that should be repeated in a specified period of time. But yes, this could
be an option. Just, that you cannot be sure, that the timeout is accurate
and therefore there could be some problems in updating a clock:

Code: Select all

Repeat
  if elapsedmilliseconds() - time > 1000
    time + 1000
    UpdateClock()
  EndIf
Until WaitWindowEvent(1000)
what if there was a user event and the second isn't over then, so Wait-
WindowEvent() could wait for another second, while the real second is
over since 0.8s? It could look a bit weird, and really weird if you use
higher timeouts, like you said 1 hour. You couldn't get around a callback/
thread/timer.

But for simple tasks, it could work. Although I will never use a timeout
of more than 500ms personally.

Code: Select all

Think of it as a forced event 0.
btw, I know how it works :P
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

AND51 wrote:Can I also use While WaitWindowEvent(0) : Wend to have your Delay(0) effect in my loop? Then I don't have to write While WindowEvent() : Delay(0) : Wend.
That should work yes, but this is PureBasic, it only takes a few seconds to try new things out, so try it :)

remi_meier wrote:For WaitWindowEvent(1000):
Actually, I personally would use a timer/callback or a thread for something
that should be repeated in a specified period of time. But yes, this could
be an option. Just, that you cannot be sure, that the timeout is accurate
and therefore there could be some problems in updating a clock:

Code: Select all

Repeat
  if elapsedmilliseconds() - time > 1000
    time + 1000
    UpdateClock()
  EndIf
Until WaitWindowEvent(1000)
what if there was a user event and the second isn't over then, so Wait-
WindowEvent() could wait for another second, while the real second is
over since 0.8s? It could look a bit weird, and really weird if you use
higher timeouts, like you said 1 hour. You couldn't get around a callback/
thread/timer.
Good point, yeah for high timeouts or accuracy one should use proper timing.
But WaitWindowEvent(1) and similar is a excellent "cheap" loop, cheap as in easy to do and hardly any code needed. (I'm sure Fred made some pretty tight code for that feature)

And you know the coolest part of it all? (something I did not notice/think about until a few weeks ago)

time.l=1
WaitWindowEvent(time)

Using a variable, you can change the variable and WaitWindowEvent() will use the new value on it's next wait.
Obviously you can do the same with Delay(time) too,
it's just something that is easy to forget you can do. (most people just slap in a 1 *laughs*)

Ah man, I think we managed to totally ruin this thread, people searching the forums are gonna be scratching their heads when this thread pops up. :)
Post Reply