我写了一个脚本来检测我何时连接了第二台显示器,并相应地切换Rainmeter布局。但是,偶尔当我让我的电脑进入睡眠状态时,将其唤醒,我会收到以下错误:
---------------------------
Windows Script Host
---------------------------
Script: C:\Users\Tim\Documents\Shortcuts\Create\scripting\commandSniffer\detectMonitor.vbs
Line: 12
Char: 2
Error: Not supported
Code: 8004100C
Source: SWbemObjectSet
---------------------------
OK
---------------------------
我真正想做的就是让我的脚本在我睡觉时不会崩溃。如果这不是一个简单的解决方案,我如何捕获脚本中的错误并忽略它?完整源代码如下:
strComputer = "Localhost"
singleMon = "myLayout"
doubleMon = "myLayout(2monitor)"
rainmeterPath = """C:\Program Files\Rainmeter\Rainmeter.exe"" !LoadLayout "
previousState = 1
set wshshell = createobject("wscript.shell")
do
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
Set colItems = objWMI.ExecQuery ("SELECT * FROM WMIMonitorID")
'Wscript.Echo strComputer & " has " & colItems.count & " monitors configured."
if not isnull(colItems) and previousState <> colItems.count then
if colItems.count = 2 then
wshshell.run rainmeterPath & doubleMon,0
else
wshshell.run rainmeterPath & singleMon,0
end if
previousState = colItems.count
else
wscript.sleep 9000
end if
wscript.sleep 1000
loop
答案 0 :(得分:0)
On Error Resume Next
将错误处理从vbscript传输给您。现在,您需要在每次可能导致错误的调用之后测试错误。
If err.number <> 0 then
'fix error or ignore
err.clear
'If decide to crash
'err.raise(err.number, blah, blah, blah)
'wscript.Quit
End If
错误处理是一个链。从最低功能调用到应用程序。 Windows查找错误处理程序,如果它找不到它崩溃。 Err.raise允许您在链中传播错误。