I have a vb.net winforms application that I want to run every time the
server starts up (it will be running on a server, and it is okay to have a
user interface). I have had a ton of trouble converting the whole project to
a windows service (tons of apartment threading issues), so I want to run it
as a scheduled task or as a simpler windows service. What I mean by a
simpler windows service is to create a simple application that just calls
the application with the correct parameters. But I need to write code on the
OnStop event in the windows service to properly shut down the application.
I saw this code on the web which can just find the name of an application
and shut it down, but I don't know if this leaves a mess behind. I have
seen a lot of problems in the posts with using Windows Scheduler. Any ideas
on the best method? Should I run KillProcess on the OnStop in the service?
Public Shared Function KillProcess(ByV al procName As String, ByVal Gentle As
Boolean)
Try
' Quickly closes any program. Can do a loop and show the names of
the processes
' if you want to figure out a process name. Just unremark the msgbox
and remark
' the if proc.processnam e.toupper block to see the names.
Dim proc As Process
Dim processes() As Process = Process.GetProc esses()
For Each proc In processes
'MsgBox(proc.Pr ocessName)
If proc.ProcessNam e.ToUpper = procName.ToUppe r Then
If Gentle Then
proc.CloseMainW indow()
' Start over if still open.
KillProcess(pro cName, Gentle)
Else
proc.Kill()
' Start over if still open.
KillProcess(pro cName, Gentle)
End If
End If
Next
Catch ex As Exception
MessageBox.Show (ex.Message, "Error", MessageBoxButto ns.OK,
MessageBoxIcon. Stop)
End Try
End Function
Derek