473,385 Members | 1,676 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,385 software developers and data experts.

Interaction of windows service with UI

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;Us er
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...icedesktop.asp

Mar 29 '07 #1
3 7421
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

"sonu" <so*************@yahoo.comwrote in message
news:11**********************@r56g2000hsd.googlegr oups.com...
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;Us er
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...icedesktop.asp

Mar 29 '07 #2
"sonu" <so*************@yahoo.comwrote in news:1175162834.651335.180280
@r56g2000hsd.googlegroups.com:
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.
Mar 29 '07 #3
On 29 Mar 2007 03:07:14 -0700, sonu wrote:
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;Us er
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...icedesktop.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
--
Bits.Bytes
http://bytes.thinkersroom.com
Mar 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: rdh | last post by:
Hi all, I am in process of developing a Server in C++ supporting multiple protocols. The server will be exposing various functionalities, and the clients can communicate over any of the...
2
by: Chakkaradeep | last post by:
hi all, as it was discussed earlier in this forum titled,"Execution of Another Application in Services", i got the answer that Services are not meant for executing another applications, but...
1
by: manuel.ricca | last post by:
Hello, I'm just starting to code a windows service in C#. My service needs to create multiple threads, each one responding to TAPI events. Then I will need to have a client connect to it for...
1
by: Rami | last post by:
Hey, Can I raise an event from a .Net Windows Service? So that if some application hooks on to my service (using Remoting) He'll be able to recieve my events? Thanks ahead
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
8
by: Rob R. Ainscough | last post by:
I have a VS 2005 Windows Service with a Installer project as part of my solution. The Service installs fine but I can't seem to make either of these work: 1. Have the service start after...
3
by: screwy | last post by:
Hi! I've been trying to figure out how to enable desktop interaction support for windows services from within my vb.net code desperatly, but wasnt able to find anything useful. Does anyone know how...
0
by: ratheeshk | last post by:
hi, i hav created one server windows service and another client windows service .The server will be listening to the specific port.but when client service is sending data to this port the exception...
5
by: sonali_reddy123 | last post by:
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...
4
by: Daniel.Benedek | last post by:
Hey there, I have a number of web services that interact with each other ( send requests to each other etc ). I would like to test the interaction between them ? Does anyone have any ideas as to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.