Interaction of windows service with UI

S

sonu

Hello all,

I am trying to develop an application which will run as a windows
service.
The application should have Normal options available with service
like start, stop and pause but along with this I need an option to
show the windows application which
my service has started as a result of its invokation.

So I have written a service control by adding a new project of type
Windows Service

below is the sample code for the class which I have written for the
service to start and work.


Public Class FirstWindowsService

Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
Me.Timer1.Enabled = True

'I have added a ProcessControl which .NET provides to execute
the process which
'I want to run as a service .
'So now in my case it is the windows application which I want
to execute.
Process1.Start()
Me.LogMessage("Service started")

End Sub

Protected Overrides Sub OnStop()
Me.Timer1.Enabled = False
Me.LogMessage("Service stoped")

Try
If Not (Process1.HasExited) Then
Process1.Kill()
End If

Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub

Protected Overrides Sub OnPause()
MyBase.OnPause()
Me.Timer1.Enabled = False
Me.LogMessage("Service Paused")
End Sub


Public Sub LogMessage(ByVal message As String)
Dim connection As SqlConnection = Nothing
Try
connection = New
SqlConnection("Server=LocalHost;Database=master;User
Id=sa;password=;")
Dim command As SqlCommand = New SqlCommand("INSERT INTO
MyServiceLog (vc_Status, dt_Created) VALUES ('" + message +
"',getdate())", connection)
connection.Open()
Dim totalRows = command.ExecuteNonQuery
Catch ex As Exception
Debug.WriteLine(ex.Message)
Finally
connection.Close()
End Try
End Sub
End Class


Now I have created a windows application which could provide me the
notifyIcon where I can put in my menus for start,stop,pause and View
the application.


below is the sample code for the class which I have written for
making this to work

'This pauses service

Private Sub PauseToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PauseToolStripMenuItem.Click
Try
ServiceController1.Pause()

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

End Sub

'This Continues paused service
Private Sub ContinueMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ContinueMenuItem.Click
Try
ServiceController1.Continue()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

'This Stops service
Private Sub StopToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
StopToolStripMenuItem.Click
Try
ServiceController1.Stop()

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

End Sub

'Enable disable proper menus
Private Sub ContextMenuStrip1_Opening(ByVal sender As
System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
Handles ContextMenuStrip1.Opening

ServiceController1.Refresh()
Try
Select Case ServiceController1.Status

Case ServiceControllerStatus.Paused

StartToolStripMenuItem.Enabled = False
PauseToolStripMenuItem.Enabled = False
StopToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = True

Case ServiceControllerStatus.Running
StartToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = False
PauseToolStripMenuItem.Enabled = True
StopToolStripMenuItem.Enabled = True

Case ServiceControllerStatus.Stopped
StartToolStripMenuItem.Enabled = True
PauseToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = False
StopToolStripMenuItem.Enabled = False

Case ServiceControllerStatus.StopPending,
ServiceControllerStatus.StartPending _
, ServiceControllerStatus.PausePending,
ServiceControllerStatus.ContinuePending

StartToolStripMenuItem.Enabled = False
PauseToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = False
StopToolStripMenuItem.Enabled = False

End Select

Catch ex As Exception
MsgBox(ex.Message)

End Try

ServiceController1.Refresh()

End Sub

I need help on how to make the application to interact with desktop. I
want to show the
application of my service when a ViewToolStripMenuItem is clicked to
show the application.

I have tried some code which manipulates the registry contents in
order to make the application to interact with desktop. It sets the
proper values but won't lauch application
What should I do?

below is the link which I have used for this is there any other
alternative?

http://www.codeproject.com/cs/system/cswindowsservicedesktop.asp
 
V

vovan

Did you try to right click on the service (installed on the machine), select
properties and mark 'Allow service to interact with desktop' on LogOn tab?
I did it during testing of the service I wrote in order to display messages.
It worked for me. I did not create any interface though.

Vovan
 
G

Guest

The application should have Normal options available with service
like start, stop and pause but along with this I need an option to
show the windows application which
my service has started as a result of its invokation.

If you need a GUI to interact with a Windows service, the best way to do
this is through remoting or a similar technology. It's a bad idea to pop a
GUI form directly from the service.
 
R

Rad [Visual C# MVP]

Hello all,

I am trying to develop an application which will run as a windows
service.
The application should have Normal options available with service
like start, stop and pause but along with this I need an option to
show the windows application which
my service has started as a result of its invokation.

So I have written a service control by adding a new project of type
Windows Service

below is the sample code for the class which I have written for the
service to start and work.

Public Class FirstWindowsService

Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
Me.Timer1.Enabled = True

'I have added a ProcessControl which .NET provides to execute
the process which
'I want to run as a service .
'So now in my case it is the windows application which I want
to execute.
Process1.Start()
Me.LogMessage("Service started")

End Sub

Protected Overrides Sub OnStop()
Me.Timer1.Enabled = False
Me.LogMessage("Service stoped")

Try
If Not (Process1.HasExited) Then
Process1.Kill()
End If

Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub

Protected Overrides Sub OnPause()
MyBase.OnPause()
Me.Timer1.Enabled = False
Me.LogMessage("Service Paused")
End Sub

Public Sub LogMessage(ByVal message As String)
Dim connection As SqlConnection = Nothing
Try
connection = New
SqlConnection("Server=LocalHost;Database=master;User
Id=sa;password=;")
Dim command As SqlCommand = New SqlCommand("INSERT INTO
MyServiceLog (vc_Status, dt_Created) VALUES ('" + message +
"',getdate())", connection)
connection.Open()
Dim totalRows = command.ExecuteNonQuery
Catch ex As Exception
Debug.WriteLine(ex.Message)
Finally
connection.Close()
End Try
End Sub
End Class

Now I have created a windows application which could provide me the
notifyIcon where I can put in my menus for start,stop,pause and View
the application.

below is the sample code for the class which I have written for
making this to work

'This pauses service

Private Sub PauseToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PauseToolStripMenuItem.Click
Try
ServiceController1.Pause()

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

End Sub

'This Continues paused service
Private Sub ContinueMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ContinueMenuItem.Click
Try
ServiceController1.Continue()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

'This Stops service
Private Sub StopToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
StopToolStripMenuItem.Click
Try
ServiceController1.Stop()

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

End Sub

'Enable disable proper menus
Private Sub ContextMenuStrip1_Opening(ByVal sender As
System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
Handles ContextMenuStrip1.Opening

ServiceController1.Refresh()
Try
Select Case ServiceController1.Status

Case ServiceControllerStatus.Paused

StartToolStripMenuItem.Enabled = False
PauseToolStripMenuItem.Enabled = False
StopToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = True

Case ServiceControllerStatus.Running
StartToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = False
PauseToolStripMenuItem.Enabled = True
StopToolStripMenuItem.Enabled = True

Case ServiceControllerStatus.Stopped
StartToolStripMenuItem.Enabled = True
PauseToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = False
StopToolStripMenuItem.Enabled = False

Case ServiceControllerStatus.StopPending,
ServiceControllerStatus.StartPending _
, ServiceControllerStatus.PausePending,
ServiceControllerStatus.ContinuePending

StartToolStripMenuItem.Enabled = False
PauseToolStripMenuItem.Enabled = False
ContinueMenuItem.Enabled = False
StopToolStripMenuItem.Enabled = False

End Select

Catch ex As Exception
MsgBox(ex.Message)

End Try

ServiceController1.Refresh()

End Sub

I need help on how to make the application to interact with desktop. I
want to show the
application of my service when a ViewToolStripMenuItem is clicked to
show the application.

I have tried some code which manipulates the registry contents in
order to make the application to interact with desktop. It sets the
proper values but won't lauch application
What should I do?

below is the link which I have used for this is there any other
alternative?

http://www.codeproject.com/cs/system/cswindowsservicedesktop.asp

When interacting with a windows service, it is better to create a second
application that interacts with the service using the service controller
class, through which you can manipulate your service.

It is not wise to have UI code directly in the service
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top