我正在运行一个与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")
答案 0 :(得分:3)
正如安斯加已经说过的那样,很明显,当PC睡眠或休眠时,什么都行不通。在PC被锁定的情况下,依赖于窗口管理或直接输入的技术(例如SendKeys)不能按预期工作,因为用户的会话以及用户级应用程序基本上被搁置到为登录屏幕或其他用户让路。
您可能想对SendMessage/PostMessage API进行一些研究,或者您可以停止使用VBScript并将其替换为使用本地服务帐户运行的计划任务或系统服务,假设您只想执行exe没有任何UI交互。
答案 1 :(得分:0)
问题是:您是否需要进入休眠状态或暂停电脑?如果没有,您应该只是配置或关闭这些设置。通过仅锁定会话,应该运行vbscript并生成一个outpout而没有任何麻烦。通过检查和配置高级窗口能量设置,它可以帮助您解决此问题。