A
Andrew
Hey all,
Requesting help from the VB.Net gurus in here. I was given a task to write
a Windows Service (VB.Net) that would run an external program, and if that
program closed for any reason (other than the service being stopped) it
would restart it. I have written the service to do just this, and for
testing purposes I am running the program Notepad. And I even have it
making entries in the System Event Log.
However, and this is my problem, I need Notepad to be seen on the desktop so
it can be interacted with. In the Services control panel, I have tried:
1) checking the box for "Interact With Desktop" for my service - no Notepad
window
2) have the service use a User Account to run - no Notepad window
3) create the service as a Local System, Local Service, and User types - no
Notepad window
4) tried adding code: myProcess.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal ... still no Notepad window.
Nothing I try seems to allow the Notepad window to show on screen. In the
Task Manager I do infact see Notepad.exe running, I'm just not able to
interact with it.
Can someone here give me some help here? The Service is doing everything I
want/need it to do, just that I need Notepad to show on screen. That is my
only stumbling point. Any help is greatly appreciated.
-- Andrew
============= My "Notepad Service" Code =============
Imports System.ServiceProcess
Public Class NPService
Inherits System.ServiceProcess.ServiceBase
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New
NPService, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New
NPService}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'NPService
'
Me.ServiceName = "NPService"
End Sub
#End Region
#Region " Private Variables"
Private myProcess As System.Diagnostics.Process
Private AllowEnd As Boolean
#End Region
#Region " Service Methods"
Protected Overrides Sub OnStart(ByVal args() As String)
StartProcess()
End Sub
Protected Overrides Sub OnStop()
AllowEnd = True
EndProcess()
End Sub
#End Region
#Region " Private Methods"
Private Sub StartProcess()
' Set process parameters
myProcess = New System.Diagnostics.Process
myProcess.StartInfo.FileName = "notepad.exe"
' Allow the process to raise events
myProcess.EnableRaisingEvents = True
' Add an Exited event handler
AddHandler myProcess.Exited, AddressOf Me.ProcessExited
' Start the process
myProcess.Start()
' Make EventLog Entry
WriteLogEntry("Process Started: " & CStr(Now()))
End Sub
Private Sub EndProcess()
' End the process
myProcess.Kill()
WriteLogEntry("Process Ended: " & CStr(Now()))
End Sub
' event handler
Friend Sub ProcessExited(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim myProcess As Process = DirectCast(sender, Process)
' Check if the process ended or if the Service is being stopped
If Not AllowEnd Then
' Make EventLog Entry
WriteLogEntry("Process Ended Abnormally at " & CStr(Now()) &
vbCrLf & "Restarting Process")
' Start the process back up again
StartProcess()
Else
' Make EventLog Entry
WriteLogEntry("Process Ended due to Service Stop at " &
CStr(Now()))
End If
End Sub
Private Sub WriteLogEntry(ByVal sText As String)
Dim MyLog As New EventLog ' create a new event log
' Check if the the Event Log Exists
If Not MyLog.SourceExists(CStr(Me.ServiceName)) Then
MyLog.CreateEventSource(CStr(Me.ServiceName),
CStr(Me.ServiceName) & " Log") ' Create Log
End If
MyLog.Source = CStr(Me.ServiceName)
' Write to the Log
MyLog.WriteEntry(CStr(Me.ServiceName) & " Log", sText,
EventLogEntryType.Information)
End Sub
#End Region
End Class
Requesting help from the VB.Net gurus in here. I was given a task to write
a Windows Service (VB.Net) that would run an external program, and if that
program closed for any reason (other than the service being stopped) it
would restart it. I have written the service to do just this, and for
testing purposes I am running the program Notepad. And I even have it
making entries in the System Event Log.
However, and this is my problem, I need Notepad to be seen on the desktop so
it can be interacted with. In the Services control panel, I have tried:
1) checking the box for "Interact With Desktop" for my service - no Notepad
window
2) have the service use a User Account to run - no Notepad window
3) create the service as a Local System, Local Service, and User types - no
Notepad window
4) tried adding code: myProcess.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal ... still no Notepad window.
Nothing I try seems to allow the Notepad window to show on screen. In the
Task Manager I do infact see Notepad.exe running, I'm just not able to
interact with it.
Can someone here give me some help here? The Service is doing everything I
want/need it to do, just that I need Notepad to show on screen. That is my
only stumbling point. Any help is greatly appreciated.
-- Andrew
============= My "Notepad Service" Code =============
Imports System.ServiceProcess
Public Class NPService
Inherits System.ServiceProcess.ServiceBase
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New
NPService, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New
NPService}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'NPService
'
Me.ServiceName = "NPService"
End Sub
#End Region
#Region " Private Variables"
Private myProcess As System.Diagnostics.Process
Private AllowEnd As Boolean
#End Region
#Region " Service Methods"
Protected Overrides Sub OnStart(ByVal args() As String)
StartProcess()
End Sub
Protected Overrides Sub OnStop()
AllowEnd = True
EndProcess()
End Sub
#End Region
#Region " Private Methods"
Private Sub StartProcess()
' Set process parameters
myProcess = New System.Diagnostics.Process
myProcess.StartInfo.FileName = "notepad.exe"
' Allow the process to raise events
myProcess.EnableRaisingEvents = True
' Add an Exited event handler
AddHandler myProcess.Exited, AddressOf Me.ProcessExited
' Start the process
myProcess.Start()
' Make EventLog Entry
WriteLogEntry("Process Started: " & CStr(Now()))
End Sub
Private Sub EndProcess()
' End the process
myProcess.Kill()
WriteLogEntry("Process Ended: " & CStr(Now()))
End Sub
' event handler
Friend Sub ProcessExited(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim myProcess As Process = DirectCast(sender, Process)
' Check if the process ended or if the Service is being stopped
If Not AllowEnd Then
' Make EventLog Entry
WriteLogEntry("Process Ended Abnormally at " & CStr(Now()) &
vbCrLf & "Restarting Process")
' Start the process back up again
StartProcess()
Else
' Make EventLog Entry
WriteLogEntry("Process Ended due to Service Stop at " &
CStr(Now()))
End If
End Sub
Private Sub WriteLogEntry(ByVal sText As String)
Dim MyLog As New EventLog ' create a new event log
' Check if the the Event Log Exists
If Not MyLog.SourceExists(CStr(Me.ServiceName)) Then
MyLog.CreateEventSource(CStr(Me.ServiceName),
CStr(Me.ServiceName) & " Log") ' Create Log
End If
MyLog.Source = CStr(Me.ServiceName)
' Write to the Log
MyLog.WriteEntry(CStr(Me.ServiceName) & " Log", sText,
EventLogEntryType.Information)
End Sub
#End Region
End Class