Results 1 to 15 of 15

Thread: VB6 DELAY pause,USED cpu %,sleep

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,808

    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

  2. #2
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    438

    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.

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,230

    Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by xiaoyao View Post
    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.

  4. #4
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,541

    Wink Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by wqweto View Post
    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?

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,199

    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.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,808

    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?

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,655

    Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by Elroy View Post
    • 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
    
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,541

    Wink Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by Niya View Post
    Code:
                Threading.Thread.Sleep(0)
    That syntax is blasphemy around here!

  9. #9
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    438

    Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by Niya View Post
    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

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,655

    Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by VanGoghGaming View Post
    That syntax is blasphemy around here!
    Oh my. I repent. I repent.

    Quote Originally Posted by vbwins View Post
    That actually looks like fun
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,808

    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.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,808

    Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by vbwins View Post
    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?

  13. #13
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,230

    Re: VB6 DELAY pause,USED cpu %,sleep

    Quote Originally Posted by xiaoyao View Post
    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.

    Quote Originally Posted by xiaoyao View Post
    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>

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,808

    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

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,808

    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
  •  



Click Here to Expand Forum to Full Width