我目前正在制作一个 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 秒睡眠/等待函数,因为 PowerPoint 没有 Application.Wait 函数。
运行此宏时,我的第 7 代 i5 上的 CPU 负载上升到 36%。自助终端计算机的硬件稍差,因此 CPU 负载会相当高,并且该 PC 的风扇会发出很大的噪音。
我认为睡眠/等待函数并不是真正的“睡眠”,它只是继续循环直到 5 秒过去。
问题 1:我关于该函数实际上并未休眠的假设是真的吗?
问题 2:如果问题 1 的答案是正确的,是否有更好的、CPU 密集程度较低的方法来创建睡眠函数?
要等待特定时间,请致电WaitMessage https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-waitmessage其次是循环中。它不是 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)