473,226 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Windows Service help (VB.Net)

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
5 2843
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Michael Riggio | last post by:
Is there a way to have a windows service instantiate a class that is a web service, which will then be accessible to clients via HTTP? Thanks, -Mike
2
by: Neslihan ERDEM | last post by:
Every body Hi first of all I say Why do I need Windows Service / Every Day I create XML file . I writed a XML web service And .I join this servis Windows service. I create Windows Service that...
2
by: Mark | last post by:
I created an extremely simple windows service that only writes to the EventLogs on Stop and Pause. I installed it using the InstallUtil.exe program, the output of which is below. It appears to be...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
0
by: Yueming | last post by:
Hello, I am using Visual Studio .NET 2003 (7.1) C++ on Windows XP. I created a project using Windows Service (.NET). I used all the default except my project name. After the project is...
1
by: Todd Jaspers | last post by:
Hey guys, I've written an NT Service. I seem to have everything it needs, an OnStart, OnStop, events. It compiles fine and I don't get any errors or warnings. My problem is that I don't have an...
2
by: zh.zhen | last post by:
i use try..catch in my windows service,and when some exception occurs, i write it to event logs. in webapplication ,if you use a catch to "hold" an exception, and do not throw up, the exception...
2
by: teejayem | last post by:
I am having problems creating a Windows Service using Visual Studio 2005. Code as follows:- Imports System.IO Imports System.Net.Mail
2
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, I have successfully developed a service (not a Webservice) using .NET 1.1 and C#. I added the requiered ProcessInstaller, debugged the service and it works. Now I want to create a setup...
0
by: kieferzhen | last post by:
I want to write a windows service (myService) by using C++/CLI (Visual Studio 2005). This myService can use MFC function for OLE Automation. Just for a first try, I did the following project...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.