0
我有以下非常簡單的腳本關閉IE並打開Chrome,只運行一次,然後崩潰
Imports System.Management
Imports System
Module Module1
Dim watcher As ManagementEventWatcher
Sub Main()
Dim monitoredProcess = "iexplore.exe"
Dim query As WqlEventQuery = New WqlEventQuery("__InstanceCreationEvent", New TimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process"" And TargetInstance.Name = """ & monitoredProcess & """")
watcher = New ManagementEventWatcher()
watcher.Query = query
watcher.Start()
watcher.WaitForNextEvent()
For Each p As Process In Process.GetProcessesByName("iexplore")
p.Kill()
Next
Process.Start(New System.Diagnostics.ProcessStartInfo("Chrome", "http://google.com"))
End Sub
End Module
打開一個IE窗口時,這應監測,關閉和運行鉻代替。這工作第一次很好,但一旦它完成它崩潰與以下錯誤
An unhandled exception of type 'System.Runtime.InteropServices.InvalidComObjectException' occurred in System.Management.dll
Additional information: COM object that has been separated from its underlying RCW cannot be used.
我該怎麼做才能阻止COM從分離?我希望程序從Windows開始,並繼續運行。
感謝
不是循環。這是一個遞歸調用,如果此程序要運行一段時間,它最終可能會導致問題。特別是堆棧溢出。你可以使用while循環來代替遞歸。 – theB
謝謝,今天監測它,看看我是否遇到任何問題。 – dave