最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to wait until the CPU usage drops below 60% in VBA? - Stack Overflow

programmeradmin5浏览0评论

The following code is using for measuring CPU % usage.

Public Sub Macro1()

Dim strComputer As String
Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor", , 48)

For Each objItem In colItems
    Debug.Print objItem.PercentProcessorTime
Next
    
End Sub

I have got the above code from this link: .

How to wait until the CPU usage drops below 60% in VBA?

The following code is using for measuring CPU % usage.

Public Sub Macro1()

Dim strComputer As String
Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor", , 48)

For Each objItem In colItems
    Debug.Print objItem.PercentProcessorTime
Next
    
End Sub

I have got the above code from this link: https://analystcave/excel-measuring-cpu-usage-in-vba-and-other-performance-metrics/#comment-184350.

How to wait until the CPU usage drops below 60% in VBA?

Share Improve this question edited Mar 18 at 15:04 Kram Kramer asked Mar 10 at 7:44 Kram KramerKram Kramer 1211 silver badge6 bronze badges 1
  • 3 The code in the post queries the WMI data only once. As far as I know, it's not possible to request streamed data from WMI. The author of the article runs the macro with a 2-second timed cron job (presumably not cron, but rather a setting within the macro). To determine when CPU usage exceeds 60%, you need to query the CPU every 1-2 seconds until the result reaches 60% or higher. – rozsazoltan Commented Mar 10 at 7:49
Add a comment  | 

2 Answers 2

Reset to default 1

you could try something like this:

(I would probably put a Sleep() in there too to help reduce it spinning to fast)

Sub Test()

    Debug.Print "Starting"
    Call PauseWhileBusy
    Debug.Print "Done"

End Sub

Sub PauseWhileBusy(Optional ByVal lMaxPercent As Long = 60, Optional ByVal strComputer As String = ".")
    
    Dim colItems As Object
    Dim objItem As Object
    Dim bTooBusy As Boolean
    
    With GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Do
            Set colItems = .ExecQuery("Select * from Win32_Processor")
            For Each objItem In colItems
                Debug.Print "Current Load:", objItem.LoadPercentage
                bTooBusy = objItem.LoadPercentage > lMaxPercent
            Next
            DoEvents
        Loop While bTooBusy
        
    End With
End Sub

You can achieve this by running a loop that continuously checks the CPU usage and only exits when it drops below 60%. To prevent excessive CPU usage while waiting, you should use Sleep to introduce a small delay between checks and DoEvents to keep the system responsive.

发布评论

评论列表(0)

  1. 暂无评论