我目前有一个 PowerPoint 演示文稿,它在计算机上用作某种信息亭或信息屏幕。
它从磁盘上的文本文件中读取它的文本。此文本文件中的文本显示在 PowerPoint 的文本框中,并且每 5 秒刷新一次。通过这种方式,我们可以编辑 PowerPoint 中的文本,而无需编辑 PowerPoint 演示文稿本身,以便它继续运行。
到目前为止效果很好,只有 PowerPoint VBA 不包含 Application.Wait 函数。在这里看到完整的子:
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 秒 sleep /等待功能,因为 PowerPoint 没有 Application.Wait 功能。
运行此宏时,我的第 7 代 i5 上的 CPU 负载高达 36%。自助服务终端计算机的硬件稍差,因此 CPU 负载会很高,并且这台 PC 的风扇会发出很大的噪音。
我认为 sleep /等待功能并没有真正“ sleep ”,它只是继续循环直到 5 秒过去。
问题 1:我的假设是该函数不会真正休眠吗?
问题 2:如果问题 1 的答案为真,是否有更好的、CPU 占用更少的方法来创建 sleep 功能?
最佳答案
要等待特定的时间,请在循环中调用 WaitMessage
后跟 DoEvents
。它不是 CPU 密集型的,UI 将保持响应:
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
关于vba - 不占用 CPU 的 PowerPoint VBA 中的 sleep /等待计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57268161/