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

Interaction of windows service with UI

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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.
<so*************@yahoo.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.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

P: n/a
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

Mar 29 '07 #3

P: n/a
In general, this is a bad idea. Services are designed to run before there is
any user logged onto the machine. So your service could be attempting to
start an executable with a UI when there is nobody logged on to even see it.
Suggest you look for a different way to accomplish your goal.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"so*************@yahoo.com" 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

Mar 29 '07 #4

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

Nirosh.

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:2D**********************************@microsof t.com...
In general, this is a bad idea. Services are designed to run before there
is
any user logged onto the machine. So your service could be attempting to
start an executable with a UI when there is nobody logged on to even see
it.
Suggest you look for a different way to accomplish your goal.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"so*************@yahoo.com" 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;U ser
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 #5

P: n/a
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
Apr 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.