Addhandler [进程] .Exited使Windows服务停止

时间:2019-05-02 09:02:36

标签: vb.net process

最近,我创建了一个Windows服务,该服务可以帮助跟踪进程的开始时间和退出时间,但是我发现,只要执行addhandler [process] .exited,Windows服务就会停止

这是我给处理程序的代码

 For Each chrome_p As Process In NewChromeProccess
            chrome_p.EnableRaisingEvents = True
            AddHandler chrome_p.Exited,
                Sub()
                    Using sw As StreamWriter = New StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\MonitoringApplication.txt", True)
                        ChromeEndTime.Add(chrome_p.ExitTime)
                        sw.WriteLine($"Process: {chrome_p.ProcessName}, Exit Time: {chrome_p.ExitTime}")
                    End Using
                End Sub

        Next
The description for Event ID 0 from source Service1 cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

Service cannot be started. An instance of the service is already running

1 个答案:

答案 0 :(得分:0)

经过一番调查,我认为您遇到的问题是由于线路造成的

sw.WriteLine($"Process: {chrome_p.ProcessName}, Exit Time: {chrome_p.ExitTime}")

尤其是chrome_p.ProcessName部分。当您在上面编写该行时,我得到了错误消息:Process has exited, so the requested information is not available。使用字符串常量将其切换出来,我在文本日志文件中得到了行Process: chrome_p.ProcessName, Exit Time: 5/2/2019 8:05:46 PM