By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,246 Members | 2,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,246 IT Pros & Developers. It's quick & easy.

Windows Service help (VB.Net)

P: n/a
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(ServicesToRu n)
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
If you use your StartProcess() method from within a non Service process (eg.
console application) does it work?

- José
"Andrew" <An********@hotmail.com> a écrit dans le message de news:
O4**************@TK2MSFTNGP15.phx.gbl...
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(ServicesToRu n)
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

Nov 21 '05 #2

P: n/a
Works perfectly as desired if I do it from a normal App vice Windows
Service. When I converted it to a Service the Notepad process goes
"invisible".

-- Andrew
"José Joye" <jo*************************************@bluewin.c h> wrote in
message news:O7**************@TK2MSFTNGP09.phx.gbl...
If you use your StartProcess() method from within a non Service process (eg. console application) does it work?

- José
"Andrew" <An********@hotmail.com> a écrit dans le message de news:
O4**************@TK2MSFTNGP15.phx.gbl...
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(ServicesToRu n)
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


Nov 21 '05 #3

P: n/a
I tried to execute your StartProcess() method from within a Service (a C#
one) and I was able to see the notepad window. However, I had to use the
local System account and check the "allow interact with desktop" checkbox.

By the way, I tested it on winxp pro sp2 (french edition)

- José
"Andrew" <An********@hotmail.com> a écrit dans le message de news:
uZ**************@tk2msftngp13.phx.gbl...
Works perfectly as desired if I do it from a normal App vice Windows
Service. When I converted it to a Service the Notepad process goes
"invisible".

-- Andrew
"José Joye" <jo*************************************@bluewin.c h> wrote in
message news:O7**************@TK2MSFTNGP09.phx.gbl...
If you use your StartProcess() method from within a non Service process

(eg.
console application) does it work?

- José
"Andrew" <An********@hotmail.com> a écrit dans le message de news:
O4**************@TK2MSFTNGP15.phx.gbl...
> 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(ServicesToRu n)
> 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
>
>



Nov 21 '05 #4

P: n/a
José

Your last sentence there provided the thing I needed. I am developing all
our .Net apps on a Win2003 Server. But when I just now tried running this
on a 2000 box, the Service worked exactly as I wanted it to. There must be
something different in the way a Service (as Local System, and Interact With
Desktop checked on) is handled between Win2k and Win2k3 Servers.

Since the production environment is a Win2k Server, I am good to go, but
will have to keep an eye open for this in the future. Again, thanks for
your help. :)

-- Andrew
"José Joye" <jo*************************************@bluewin.c h> wrote in
message news:ub**************@TK2MSFTNGP09.phx.gbl...
I tried to execute your StartProcess() method from within a Service (a C#
one) and I was able to see the notepad window. However, I had to use the
local System account and check the "allow interact with desktop" checkbox.

By the way, I tested it on winxp pro sp2 (french edition)

- José
"Andrew" <An********@hotmail.com> a écrit dans le message de news:
uZ**************@tk2msftngp13.phx.gbl...
Works perfectly as desired if I do it from a normal App vice Windows
Service. When I converted it to a Service the Notepad process goes
"invisible".

-- Andrew
"José Joye" <jo*************************************@bluewin.c h> wrote in message news:O7**************@TK2MSFTNGP09.phx.gbl...
If you use your StartProcess() method from within a non Service process

(eg.
console application) does it work?

- José
"Andrew" <An********@hotmail.com> a écrit dans le message de news:
O4**************@TK2MSFTNGP15.phx.gbl...
> 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(ServicesToRu n)
> 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
>
>



Nov 21 '05 #5

P: n/a
I take back what I said....it is not the difference in OS's, but rather it
was that I was connecting to my development box via Terminal Service. It
seems that you cannot have an interface with a service when you are
connected to the machine via Terminal Service. Well, not without a pretty
exstensive amount of low-level code. There is an article on this here:
http://msdn.microsoft.com/library/de...e_services.asp

In short... the service I wrote, the secondary program can only be
interacted with from the machine itself, *not* through Terminal Services.
Fine with me, just took some research and testing to determine this. Your
comments were still a contributing factor in helping me solve the problem.
:)

Thanks again

-- Andrew
"Andrew" <An********@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP14.phx.gbl...
José

Your last sentence there provided the thing I needed. I am developing all
our .Net apps on a Win2003 Server. But when I just now tried running this
on a 2000 box, the Service worked exactly as I wanted it to. There must be something different in the way a Service (as Local System, and Interact With Desktop checked on) is handled between Win2k and Win2k3 Servers.

Since the production environment is a Win2k Server, I am good to go, but
will have to keep an eye open for this in the future. Again, thanks for
your help. :)

-- Andrew
"José Joye" <jo*************************************@bluewin.c h> wrote in
message news:ub**************@TK2MSFTNGP09.phx.gbl...
I tried to execute your StartProcess() method from within a Service (a C#
one) and I was able to see the notepad window. However, I had to use the local System account and check the "allow interact with desktop" checkbox.
By the way, I tested it on winxp pro sp2 (french edition)

- José
"Andrew" <An********@hotmail.com> a écrit dans le message de news:
uZ**************@tk2msftngp13.phx.gbl...
Works perfectly as desired if I do it from a normal App vice Windows
Service. When I converted it to a Service the Notepad process goes
"invisible".

-- Andrew
"José Joye" <jo*************************************@bluewin.c h> wrote
in message news:O7**************@TK2MSFTNGP09.phx.gbl...
> If you use your StartProcess() method from within a non Service process (eg.
> console application) does it work?
>
> - José
> "Andrew" <An********@hotmail.com> a écrit dans le message de news:
> O4**************@TK2MSFTNGP15.phx.gbl...
> > 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(ServicesToRu n)
> > 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
> >
> >
>
>



Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.