【发布时间】:2017-07-24 19:12:48
【问题描述】:
我正在运行一个与 Windows 7 中的 exe 文件通信的 VBScript。
VBScript 非常好用!
我遇到的问题是,一旦 PC 被锁定、进入睡眠或休眠状态,VBScript 就无法与 exe 应用程序通信。
VBScript 正在运行(我有一个日志,每次循环完成时都会告诉我,但它没有与 exe 通信。
以下代码在 PC 被锁定时不起作用。
Set WSHShell = WScript.CreateObject("WScript.Shell")
' info for exporting data
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine, cycles
Dim I
I = 0
Dim n
n = .1 'how often the program saves the data (in minutes)
cycles = 2 'how many times it will save
FileName = "C:\Users\Desktop\new.txt" 'location where the log file will save
Dim sl
sl = n * 60000 'change from seconds to ms for the sleep function
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output
Set MyFile = fso.OpenTextFile(FileName, ForAppending, True, TristateTrue)
' Write to the file.
MyFile.WriteLine "Log file for recording data from Yokogawa MX100 (" & cycles
& " cycles)"
WSHShell.Run "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.AppActivate "MXStandardE.exe"
WScript.Sleep 1000
Do while I < cycles
a = Now()
WScript.Sleep 1000
WSHShell.Run "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.AppActivate "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.SendKeys "%A"
WScript.Sleep 1000
WSHShell.SendKeys "{DOWN}"
WScript.Sleep 1000
WSHShell.SendKeys "{ENTER}"
I = I + 1
MyFile.Writeline I & " of " & cycles & " at " & a & " --time of each cycle is
" & n & " minutes"
WScript.Sleep sl 'when sl is used loop time is in minutes
Loop
MyFile.Close
MsgBox ("Script has completed")
【问题讨论】:
-
当 PC 进入睡眠模式或休眠状态时,操作会暂停,因此很明显,在这种情况下,您的脚本操作将被中断(否则 PC 不会首先处于睡眠/休眠状态) .不过,简单地锁定屏幕不应该有相同的效果(除非计算机在被锁定时立即进入睡眠模式)。您的脚本“交流”的程度如何准确?当PC被锁定时,你怎么知道它没有?你能展示更多你的代码吗?您问题中的 sn-p 没有显示任何应该通过锁定屏幕来中断的内容。
-
感谢 Ansgar,我编辑了原始问题并添加了完整的代码。 “Alt、Down、Enter”功能用于保存来自 DAQ 的数据。当我检查网络时,我可以看到它没有保存,但日志正在写入文本文件。如果我看到应用程序没有保存,我可以去 PC 解锁并且应用程序再次保存。再次感谢,-Verrell