Announcement

Collapse
No announcement yet.

Application.Sleep and CPU

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Application.Sleep and CPU

    Hi, Why Application.Sleep() is consuming the processor too much?
    What else can I use instead of Application.Sleep(). Thanks

  • #2

    image widget

    my code example
    Application.Sleep(10000) ;10 second

    Attached Files

    Comment


    • #3
      I don't know what Application.Sleep is doing to be honest, I thought it would just call Kernel32!Sleep, but it appears to do something else.
      Anyhow, put this in globals:
      Code:
      function Sleep(timeInMilliseconds)
          assert(type(timeInMilliseconds) == "number", "A numeric time in milliseconds is required for Sleep()");
          DLL.CallFunction(_SystemFolder .. "\\Kernel32.dll", "Sleep", tostring(timeInMilliseconds), DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
      end
      usage:
      Code:
      Sleep(10000);
      API: https://docs.microsoft.com/en-us/win...synchapi-sleep
      Bas Groothedde
      Imagine Programming :: Blog

      AMS8 Plugins
      IMXLH Compiler

      Comment


      • #4
        Originally posted by Imagine Programming View Post
        I don't know what Application.Sleep is doing to be honest, I thought it would just call Kernel32!Sleep, but it appears to do something else.
        Anyhow, put this in globals:
        Code:
        function Sleep(timeInMilliseconds)
        assert(type(timeInMilliseconds) == "number", "A numeric time in milliseconds is required for Sleep()");
        DLL.CallFunction(_SystemFolder .. "\\Kernel32.dll", "Sleep", tostring(timeInMilliseconds), DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
        end
        usage:
        Code:
        Sleep(10000);
        API: https://docs.microsoft.com/en-us/win...synchapi-sleep
        I am grateful to you, thank you very, very much..
        that worked smoothly

        function Sleep(timeInMilliseconds)
        DLL.CallFunction(_SystemFolder .. "\\Kernel32.dll", "Sleep", tostring(timeInMilliseconds), DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
        end

        Comment


        • #5
          Originally posted by mixim View Post

          I am grateful to you, thank you very, very much..
          that worked smoothly

          function Sleep(timeInMilliseconds)
          DLL.CallFunction(_SystemFolder .. "\\Kernel32.dll", "Sleep", tostring(timeInMilliseconds), DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
          end
          You're welcome, but I don't understand why you removed the 'assert' line from the code.
          That assert line makes sure that the provided argument is actually a number, to prevent erroneous input to Kernel32!Sleep.
          Is there a reason for it?
          Bas Groothedde
          Imagine Programming :: Blog

          AMS8 Plugins
          IMXLH Compiler

          Comment


          • #6
            Originally posted by Imagine Programming View Post

            You're welcome, but I don't understand why you removed the 'assert' line from the code.
            That assert line makes sure that the provided argument is actually a number, to prevent erroneous input to Kernel32!Sleep.
            Is there a reason for it?
            I'm glad to be here. Sorry, i didn't know this.
            I had removed it because I did not encountered an error message.
            Ok, i added it back to my code line. Thank you again for the information and warning.

            Comment


            • #7
              Nice.. i also notice this but i though it was just normal...
              one other thing make a lot of CPU usage is the FOR LOOP
              function Loop(t)

              for i=1, t do
              Paragraph.SetText("Paragraph1", i)
              end

              end

              Loop(10000)

              Comment


              • #8
                I think it's normal for for,while,until.
                But i sure something is wrong for Sleep

                Comment


                • #9
                  mixim Thanks for confirmation...

                  Comment


                  • #10
                    Originally posted by mixim View Post
                    I think it's normal for for,while,until.
                    But i sure something is wrong for Sleep
                    Yes, Application.Sleep should not use so much CPU. I agree something is wrong with it.
                    Ulrich do you know how Application.Sleep is implemented in the AMS engine?
                    Bas Groothedde
                    Imagine Programming :: Blog

                    AMS8 Plugins
                    IMXLH Compiler

                    Comment


                    • #11
                      No, I do not have access to any source code of Indigo Rose products.

                      Ulrich

                      Comment


                      • #12
                        Originally posted by Ulrich View Post
                        No, I do not have access to any source code of Indigo Rose products.

                        Ulrich
                        Too bad, it would've been an interesting anecdote to this thread haha
                        Bas Groothedde
                        Imagine Programming :: Blog

                        AMS8 Plugins
                        IMXLH Compiler

                        Comment


                        • #13
                          I've looked at the implementation of Application.Sleep through debugging and noticed that it tries to process window messages while it's sleeping in a tight loop.

                          So, as it seems, Application.Sleep is not just an implementation of Sleep. Rather, it's a custom implementation that uses WINMM.DLL!timeGetTime, USER32.DLL!PeekMessageA, USER32.DLL!TranslateMessage and USER32.DLL!DispatchMessageA to keep processing messages.

                          This also seems to be the reason why it's using up 100% of the core the UI thread is running on, as it's a tight loop without handing over CPU time back to the system. Hence that it used 25% of a quad-core CPU, for it me it used 3% on a 16-core CPU with HT (32 logical).
                          Bas Groothedde
                          Imagine Programming :: Blog

                          AMS8 Plugins
                          IMXLH Compiler

                          Comment


                          • #14
                            Originally posted by Imagine Programming View Post
                            I've looked at the implementation of Application.Sleep through debugging and noticed that it tries to process window messages while it's sleeping in a tight loop.

                            So, as it seems, Application.Sleep is not just an implementation of Sleep. Rather, it's a custom implementation that uses WINMM.DLL!timeGetTime, USER32.DLL!PeekMessageA, USER32.DLL!TranslateMessage and USER32.DLL!DispatchMessageA to keep processing messages.

                            This also seems to be the reason why it's using up 100% of the core the UI thread is running on, as it's a tight loop without handing over CPU time back to the system. Hence that it used 25% of a quad-core CPU, for it me it used 3% on a 16-core CPU with HT (32 logical).

                            I don't speak very English. So I will try to explain simple.


                            function Sleep(timeInMilliseconds)
                            assert(type(timeInMilliseconds) == "number", "A numeric time in milliseconds is required for Sleep()");
                            DLL.CallFunction(_SystemFolder .. "\\Kernel32.dll", "Sleep", tostring(timeInMilliseconds), DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
                            end

                            for example for that func, CPU usage is normal but the same problem exists in this too.

                            I guess, main reason is not the Application.Sleep() command, it is related to the AMS dynamic.

                            Comment


                            • #15
                              Originally posted by mixim View Post


                              I don't speak very English. So I will try to explain simple.


                              function Sleep(timeInMilliseconds)
                              assert(type(timeInMilliseconds) == "number", "A numeric time in milliseconds is required for Sleep()");
                              DLL.CallFunction(_SystemFolder .. "\\Kernel32.dll", "Sleep", tostring(timeInMilliseconds), DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
                              end

                              for example for that func, CPU usage is normal but the same problem exists in this too.

                              I guess, main reason is not the Application.Sleep() command, it is related to the AMS dynamic.
                              In my testing, Kernel32!Sleep uses 0% CPU. Can you show me how you use the function?
                              Bas Groothedde
                              Imagine Programming :: Blog

                              AMS8 Plugins
                              IMXLH Compiler

                              Comment

                              Working...
                              X