Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was created a windows service, now I am trying to create a GUI for a service through which i am able to start, stop, restart and pause service. My requirement is "I want to add a form in service,with four buttons."
How to add form in windows service and how to interact between form and service?
Is it possible to add GUI to windows service?
Posted

As Abhinav explained, you can not add UI to Windows Service.

What you can do is developing a separate Windows application to interact with your Service. To implement the operations you need, this application should use the class System.ServiceProcess.ServiceController, please see:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx[^].

—SA
 
Share this answer
 
Comments
Abhinav S 6-Feb-12 2:26am    
My 5.
Sergey Alexandrovich Kryukov 6-Feb-12 2:30am    
Thank you, Abhinav.
--SA
Avinash6474 6-Feb-12 3:08am    
Thank you SAKryukov,I did what I want. I added a windows project in same application and write a code on click events. It is working fine.
-Avinash
Sergey Alexandrovich Kryukov 6-Feb-12 3:15am    
Great, my congratulations.
And you are very welcome.

Good luck, call again.
--SA
thatraja 6-Feb-12 5:01am    
Right, 5!
By default, services use a noninteractive window station and cannot interact with the user. However, an interactive service can display a user interface and receive user input.

Caution: Services running in an elevated security context, such as the LocalSystem account, should not create a window on the interactive desktop because any other application that is running on the interactive desktop can interact with this window. This exposes the service to any application that a logged-on user executes. Also, services that are running as LocalSystem should not access the interactive desktop by calling the OpenWindowStation or GetThreadDesktop function.

To create an interactive service, do the following when calling the CreateService function:
1.Specify NULL for the lpServiceStartName parameter to run the service in the context of the LocalSystem account.

2.Specify the SERVICE_INTERACTIVE_PROCESS flag.

To determine whether a service is running as an interactive service, call the GetProcessWindowStation function to retrieve a handle to the window station, and the GetUserObjectInformation function to test whether the window station has the WSF_VISIBLE attribute.

However, note that the following registry key contains a value, NoInteractiveServices, that controls the effect of SERVICE_INTERACTIVE_PROCESS:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows

The NoInteractiveServices value defaults to 0, which means that services with SERVICE_INTERACTIVE_PROCESS are allowed to run interactively. When NoInteractiveServices is set to a nonzero value, no service started thereafter is allowed to run interactively, regardless of whether it has SERVICE_INTERACTIVE_PROCESS.

Important: All services run in Terminal Services session 0. Therefore, if an interactive service displays a user interface, it is visible only to the user who connected to session 0. Because there is no way to guarantee that the interactive user is connected to session 0, do not configure a service to run as an interactive service under Terminal Services or on a system that supports fast user switching (fast user switching is implemented using Terminal Services).

You need to execute the Application.Run in one thread only.

So it's possible to create an interactive service, but usually not worth the effort.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Feb-12 9:50am    
Very interesting, good to know. I did not try such thing (please see my comment to your comment to "solution 1"). Thank you for sharing. My 5.
--SA
Espen Harlinn 6-Feb-12 9:51am    
As I mention, it's possible - but usually not worth the effort ...
Sergey Alexandrovich Kryukov 9-Feb-12 12:41pm    
Oh, I certainly agree with that. One can always embed some IPC to collaborate with yet another, interactive application and have much more flexibility and more straightforward and maintainable development.
--SA
thatraja 6-Feb-12 9:59am    
5!
Espen Harlinn 6-Feb-12 10:09am    
thank you, thatraja :)
It is not possible to add GUI to windows service.
However, you can allow the UI to communicate with the existing service.

You can always add a control to dynamically - so for adding a Windows Form, see here[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Feb-12 2:21am    
Correct, but this is only the half of required answer, maybe a bit more. OP asked "how to communicate". Last link is almost irrelevant.

So, I voted 4 this time.
Please also see my answer, more complete :-)
--SA
Abhinav S 6-Feb-12 2:26am    
Thanks SA.
Avinash6474 6-Feb-12 3:09am    
Thank you Abhinav, I got what I want.

-Avinash
Abhinav S 6-Feb-12 5:04am    
You are welcome.
thatraja 6-Feb-12 5:01am    
5!
As others have mentioned, be cautious when designing an interactive windows service. See this entry in the Windows Services FAQ for a full discussion on the topic:
http://www.coretechnologies.com/WindowsServices/FAQ.html#GUIServices[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900