这是我想要做的是创建一个检查 Microsoft Lync 是否正在运行的服务。如果它正在运行,那么除了写入事件日志之外什么都不做。如果它没有运行,请运行 exe,然后登录到 Lync。我遇到的问题是,当需要运行 exe 时,它会启动该进程,但它从未真正运行过该应用程序。我试图查看是否可以使用记事本,但它所做的只是在任务管理器中创建进程,但从未打开实际的应用程序。
Imports System
Imports System.Data
Imports System.Timers
Imports System.Diagnostics
Imports System.Data.SqlClient
Imports System.ServiceProcess
Imports System.Windows.Forms
Public Class Service1
Protected Overrides Sub OnStart(ByVal args() As String)
EventLog.WriteEntry("In Onstart", "starting timer")
Timer1.Start()
End Sub
Protected Overrides Sub OnStop()
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object,
ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
If IsProcessRunning("communicator") Then
EventLog.WriteEntry("no problem")
Else
EventLog.WriteEntry("not running")
Dim info As New ProcessStartInfo("C:\Program Files (x86)\Microsoft Lync\communicator.exe")
info.UseShellExecute = False
info.RedirectStandardError = True
info.RedirectStandardInput = True
info.RedirectStandardOutput = True
info.CreateNoWindow = True
info.ErrorDialog = False
info.WindowStyle = ProcessWindowStyle.Hidden
Dim process__1 As Process = Process.Start(info)
End If
End Sub
Public Function IsProcessRunning(ByVal name As String) As Boolean
For Each clsProcess As Process In Process.GetProcesses()
If clsProcess.ProcessName.StartsWith(name) Then
Return True
End If
Next
Return False
End Function
End Class