-
Apr 22nd, 2024, 07:01 PM
#1
Thread Starter
PowerPoster
VB6 DELAY pause,USED cpu %,sleep
SleepDoEvents:
DOEVENTS+SLEEP 1, almost no CPU runtime
DelayDoEvents:
No runtime delay, 9% CPU usage (average 1.6% per core)
doevents loop - The actual CPU usage is about 18%, and the displayed CPU usage is 29%
6-core CPU, 17% per core at full load (15.5% load increase)
Why is only 3% of process usage displayed inside?
DelayLoop:
No running delay, 5% CPU usage (average 1% per core)
Running without SPLEEP delays, the CPU takes up 24%, and the actual CPU takes up about 19%
Only 1.5% of process usage is displayed inside
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '-----??N????
Declare Function timeGetTime Lib "winmm.dll" () As Long
Sub SleepDoEvents(Ms As Long) 'Delay a few milliseconds without locking the CPU
Dim Time1Max As Long
Time1Max = timeGetTime + Ms
While timeGetTime < Time1Max
DoEvents
Sleep 1
Wend
End Sub
Sub DelayDoEvents(Ms As Long) 'Delay a few milliseconds card CPU, display occupies 3%
Dim Time1Max As Long
Time1Max = timeGetTime + Ms
While timeGetTime < Time1Max
DoEvents
Wend
End Sub
Sub DelayLoop(Ms As Long) 'Delay a few milliseconds, card CPU, display occupies 1.5%
Dim Time1Max As Long
Time1Max = timeGetTime + Ms
While timeGetTime < Time1Max
'
Wend
End Sub
-
Apr 23rd, 2024, 02:24 AM
#2
Hyperactive Member
Re: VB6 DELAY pause,USED cpu %,sleep
What problem are you trying to solve?
Note that timeGetTime wraps every 49.71 days approx. If you enter one of these sub's and the time wraps you will be in for a long wait.
-
Apr 23rd, 2024, 03:44 AM
#3
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by xiaoyao
Why is only 3% of process usage displayed inside?
No idea what you are asking here (another brain fart from the auto-translator) but almost certainly DoEvents has some "cleverness" implemented to peek message queue and if there are no outstanding messages it just bails out.
The effect is that calling DoEvents in a loop does not sleep the current thread as on most of the executions there are no messages in current threads message queue. So DelayDoEvents becomes DelayLoop for 99.99% of the iterations i.e. thread is not releasing its quant of time to OS so CPU is busy.
Calling DoEvent + Sleep is the way to go to reduce CPU usage to 0% *and* wreak havoc to your UI code by introducing reentracy problems on every button, every menu, every combo click/change etc. Way to go! (NOT)
cheers,
</wqw>
p.s. Over and out. Will skip/hide next 100 posts from this acount.
-
Apr 23rd, 2024, 04:37 AM
#4
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by wqweto
p.s. Over and out. Will skip/hide next 100 posts from this acount.
Yeah, that'll take a couple of days or so! How are you counting them though?
-
Apr 23rd, 2024, 07:18 AM
#5
Re: VB6 DELAY pause,USED cpu %,sleep
Yeah, I don't understand the question either. Here's my understanding:
- A perpetual loop with nothing in it: This holds the thread, continuously running its loop, and is rough on the CPU.
- Sleep: This releases the thread back to Windows, but doesn't allow subsequent events to be raised. Therefore, Sleep in a loop is probably easiest on the CPU.
- DoEvents: Keeps the thread. If in a loop, it's probably about the same as a perpetual loop with nothing. Only difference being that other events may execute and then return to the perpetual loop.
Not sure there's much more to it than that.
Bottom line:
- There are virtually no cases where a do-nothing loop should be used to pause execution.
- If you need an absolute pause that suspends program execution for some period of time, use Sleep. That's what it's for.
- There are extremely few cases where DoEvents makes any sense (in a loop or not), none of which you've mentioned.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Apr 24th, 2024, 12:44 AM
#6
Thread Starter
PowerPoster
Re: VB6 DELAY pause,USED cpu %,sleep
WHILE enabled
DOEVENTS
WEND
I see that the CPU usage in the task manager has changed from 5% to 25%, but the CPU usage of the process is only showing 2-3%. Where does the CPU of the middle 20% go, or what tools can be used to accurately display it?
-
Apr 25th, 2024, 03:17 AM
#7
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by Elroy
- Sleep: This releases the thread back to Windows, but doesn't allow subsequent events to be raised. Therefore, Sleep in a loop is probably easiest on the CPU.
Fun fact: Sleep(0) is a nice way to yield the rest of your thread's time slice to the OS. I found it incredibly useful as a way to reduce CPU usage without adding unnecessary latency in worker threads running polling loops.
Eg.
Code:
Do Until SomethingHappens()
'Yield this thread to the OS. We can wait till
'scheduler puts us back on to poll again.
Threading.Thread.Sleep(0)
Loop
-
Apr 25th, 2024, 04:02 AM
#8
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by Niya
Code:
Threading.Thread.Sleep(0)
That syntax is blasphemy around here!
-
Apr 25th, 2024, 04:31 AM
#9
Hyperactive Member
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by Niya
Fun fact: Sleep(0) is a nice way to yield the rest of your thread's time slice to the OS. I found it incredibly useful as a way to reduce CPU usage without adding unnecessary latency in worker threads running polling loops.
Eg.
Code:
Do Until SomethingHappens()
'Yield this thread to the OS. We can wait till
'scheduler puts us back on to poll again.
Threading.Thread.Sleep(0)
Loop
Other fun facts
https://learn.microsoft.com/en-us/wi...nchapi-sleepex
-
Apr 25th, 2024, 10:31 AM
#10
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by VanGoghGaming
That syntax is blasphemy around here! 
Oh my. I repent. I repent. 
 Originally Posted by vbwins
That actually looks like fun
-
Apr 26th, 2024, 06:18 AM
#11
Thread Starter
PowerPoster
Re: VB6 DELAY pause,USED cpu %,sleep
WaitableTimer+MsgWaitForMultipleObjects
This API uses almost no CPU if no events are generated. If you keyboard type or drag the form,
it will result in dozens to hundreds of calls to DoeventS.
DOEVENTS does not fire without any mouse or keyboard action.
(Normal While+doevents without SLEEP, accounting for 1.38 million Doevents calls)
Code:
While timeGetTime < Time1Max
DoEvents
N = N + 1
Wend
Use SLEEP 1+doevents or sleep(0), as long as it does not take up CPU. This is a delay function implementation method with the least amount of code.
However, the main purpose of this post is to find out why the process CPU usage is inaccurate.
Code:
pause(1000)
sub pause(x as long)
****
lRet = SetWaitableTimer(mlTimer, ft, 0, 0, 0, False)
lBusy = -1
Do While (lBusy <> WAIT_OBJECT_0 And Not EndExe)
lBusy = MsgWaitForMultipleObjects(1, mlTimer, False, INFINITE, QS_ALLINPUT&)
If lBusy < 1 Then
Exit Do
Else
DoEvents
End If
Loop
Last edited by xiaoyao; Apr 26th, 2024 at 06:23 AM.
-
Apr 26th, 2024, 06:25 AM
#12
Thread Starter
PowerPoster
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by vbwins
What problem are you trying to solve?
Note that timeGetTime wraps every 49.71 days approx. If you enter one of these sub's and the time wraps you will be in for a long wait.
which api is best?gettickcount?
-
Apr 26th, 2024, 06:30 AM
#13
Re: VB6 DELAY pause,USED cpu %,sleep
 Originally Posted by xiaoyao
Use SLEEP 1+doevents or sleep(0), as long as it does not take up CPU. This is a delay function implementation method with the least amount of code.
Not true. Your snippet is missing half the code.
 Originally Posted by xiaoyao
However, the main purpose of this post is to find out why the process CPU usage is inaccurate.
Not true. There is no inaccuracy.
Btw, you are leaking the timer i.e. missing call to CloseHandle.
cheers,
</wqw>
-
Apr 26th, 2024, 06:46 AM
#14
Thread Starter
PowerPoster
Re: VB6 DELAY pause,USED cpu %,sleep
the best?vb6 delay)pause,doevents(no need cpu)-VBForums
https://www.vbforums.com/showthread.php?882803
Code:
Sub CloseTimerW()
CloseHandle hTimer
End Sub
-
Apr 26th, 2024, 06:59 AM
#15
Thread Starter
PowerPoster
Re: VB6 DELAY pause,USED cpu %,sleep
Perhaps the CPU virtualization function is enabled and virtual server functions such as hyper-v are running.
The CPU usage per process is not accurate. For example, Hyper-V.Xe may only have 1% CPU usage, but the CPU usage of the virtual machine inside may be 50-80%
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|