关闭 IE 并打开 Chrome,只运行一次然后崩溃
Close IE and Open Chrome, only runs once and then crashes
我有以下非常简单的脚本
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 window 打开时应该监视它,关闭它并改为 运行 chrome。第一次运行时效果很好,但一旦完成就会崩溃并出现以下错误
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 开始并一直持续到 运行。
谢谢
我修复了这个基本上是创建一个循环
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"))
watcher.Stop()
watcher.Dispose()
Main()
End Sub
End Module
我有以下非常简单的脚本
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 window 打开时应该监视它,关闭它并改为 运行 chrome。第一次运行时效果很好,但一旦完成就会崩溃并出现以下错误
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 开始并一直持续到 运行。
谢谢
我修复了这个基本上是创建一个循环
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"))
watcher.Stop()
watcher.Dispose()
Main()
End Sub
End Module