Interaction of windows service with UI

S

sonali_reddy123

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
 
C

Champika Nirosh

Your question is not clear.. when you say "I need an option to show the
windows application which my service has started as a result of its
invokation." I am not getting a meaning there.. specially since your OnStart
method's code is missing.. but any way if you are trying to fire a Windows
Form application via your windows service.. I doubt whether it is possible..

Nirosh.
 
A

auratius

Are you looking for an application that will allow a small application
in the toolbar to appear when the service is startef or when it
executes at timed interval?

If so there will be a huge security hole. It can be done via the
ServiceInstaller.cs-- but Microsoft are not going to support his
option in Vista (we shall see). But I recommend that you avoid using
Interact with Desktop as it is NOT reliable if your service shells or
uses Diagnostics.Process.

Regards

http://www.auratius.co.za

Auratius
 
C

Champika Nirosh

Yes it is a bad idea but still is it possible??
I remember it is not possible, isn't that so??

Nirosh.
 
F

Francesco

I have done this with a separate thread.
You can implement your application as a service, then in the OnStart event of the service create a secondary thread and start the user interface.

This will work only if the service is installed and configured to interact with desktop, and only if the service connection account is LocalSystem.
To achieve this you have to use the Windows APIS to register the service.
Please be aware that the LocalSystem user (in taskmanager you can see it as SYSTEM) will be not trusted in network, so no network printer or network directory share will be available to this user.
It is not easy to implement a service in this way, so do it only if it is strictly needed.

So the OnStart event should be:

protected override void OnStart(string[] args)
{
System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(StartUserInterface));
thread.IsBackground=true;
thread.Name=this.ServiceName+"UserInterface";
thread.Start();
}

And the StartUserInterface method should be:

private void StartUserInterface()
{
System.Windows.Forms.Application.Run(new Form1());
}



EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 

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