本文介绍了不占用 CPU 的 PowerPoint VBA 中的睡眠/等待计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 PowerPoint 演示文稿,它在计算机上用作某种信息亭或信息屏幕.它从磁盘上的文本文件中读取它的文本.此文本文件中的文本显示在 PowerPoint 的文本框中,并且每 5 秒刷新一次.通过这种方式,我们可以编辑 PowerPoint 中的文本,而无需编辑 PowerPoint 演示文稿本身,以便它继续运行.到目前为止效果很好,只有 PowerPoint VBA 不包含 Application.Wait 函数.在这里看到完整的子:

I'm currently having a PowerPoint presentation that's being used on a computer as some sort of kiosk or information screen.It reads it's text from a text file on the disk. The text in this text file is displayed in a textbox in PowerPoint and this is being refresh every 5 seconds. This way we can edit the text in the PowerPoint without editing the PowerPoint presentation itself so it will continue to run.Work great so far, only PowerPoint VBA does not contain the Application.Wait function. See here the full sub:

Sub Update_textBox_Inhoud()

Dim FileName As String
TextFileName = "C:\paht\to\textfile.txt"
If Dir$(FileName) <> "" Then

Application.Presentations(1).SlideShowSettings.Run
Application.WindowState = ppWindowMinimized


While True


    Dim strFilename As String: strFilename = TextFileName
    Dim strFileContent As String
    Dim iFile As Integer: iFile = FreeFile
    Open strFilename For Input As #iFile
    strFileContent = Input(LOF(iFile), iFile)
    Application.Presentations(1).Slides(1).Shapes.Range(Array("textBox_Inhoud")).TextFrame.TextRange = strFileContent
    Close #iFile


    waitTime = 5
    Start = Timer
    While Timer < Start + waitTime
        DoEvents
    Wend

Wend

Else

End If
End Sub

如您所见,我在循环中创建了一个循环来创建 5 秒睡眠/等待功能,因为 PowerPoint 没有 Application.Wait 功能.

As you can see I've got a loop within a loop to create a 5 second sleep / wait function, as PowerPoint doesn't have a Application.Wait function.

运行此宏时,我的第 7 代 i5 上的 CPU 负载高达 36%.kiosk 电脑硬件稍差,所以 CPU 负载会很高,而且这台电脑的风扇会发出很大的噪音.

While running this macro my CPU load on my 7th gen i5 goes up to 36%. The kiosk computer has slightly worse hardware so the CPU load will be quite high and the fan of this PC will make a lot of noise.

我认为睡眠/等待功能并没有真正睡眠",它只是继续循环直到 5 秒过去.

I think the sleep / wait function doesn't really "sleep", it just continues to loop until 5 seconds have past.

问题 1:我的假设是函数没有真正休眠吗?问题 2:如果问题 1 的答案为真,是否有更好的、CPU 占用更少的方法来创建睡眠功能?

Question 1 : Is my assumption that the function doesn't really sleep true?Question 2 : If the answer to question 1 is true, is there a better, less CPU intensive way, to create a sleep function?

推荐答案

要等待特定的时间,请调用 WaitMessage 后跟 DoEvents 在一个循环中.它不是 CPU 密集型的,用户界面将保持响应:

To wait for a specific amount of time, call WaitMessage followed by DoEvents in a loop. It's not CPU intensive and the UI will remain responsive:

Private Declare PtrSafe Function WaitMessage Lib "user32" () As Long


Public Sub Wait(Seconds As Double)
    Dim endtime As Double
    endtime = DateTime.Timer + Seconds
    Do
        WaitMessage
        DoEvents
    Loop While DateTime.Timer < endtime
End Sub

这篇关于不占用 CPU 的 PowerPoint VBA 中的睡眠/等待计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:34