Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a window service and on "On Start" event i want to run a Window application. By using the below code i am able to initiate the application, but not able to see the Window forms. I can see the process running in Task Manager. Plz Help.
Thanks.
C#
ProcessStartInfo pInfo = new ProcessStartInfo(Path);
pInfo.WindowStyle = ProcessWindowStyle.Maximized;
Process.Start(pInfo);
Posted
Updated 8-Apr-11 0:27am
v2

You are going to get problems if you do that. A windows service is designed to run processes that don't require a GUI (Graphical User Interface). For that reason, a service doesn't even support a GUI. In order to run software with a GUI you're required to have a logged on user. I recommend starting the Windows Forms application in a different way.

If you do want to try to start a process from a service, try the Process object in the System.Diagnostics namespace.

Good luck,
Eduard
 
Share this answer
 
Comments
Ashwani Dhiman 8-Apr-11 7:26am    
Thanks for Reply.

I have to initiate a client application automatically as the server application start. For detecting the server application running or not, i was using the Window Service. But through Window service i am not able to start client application. If server application is not running, client application can not be start even by the user. Can you suggest me what i have to use to initiate client application? Thanks.
Albin Abel 8-Apr-11 11:41am    
My 5
Sergey Alexandrovich Kryukov 9-Apr-11 1:15am    
That is correct, my 5. OP could use much better approached to tackle the problem.
--SA
It is not possible. Windows Service program runs under "Session Id 0", where the first logged in user runs in "Session Id 1". There is no user logged in under "Session Id 0", where your Windows Service runs. This is true for Windows Vista and above. For Windows XP, Windows Service runs under the same "Session Id" as the first logged ind user.

Better way is to use "Startup folder" or registry under "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"


See link Session 0 Isolation[^]
 
Share this answer
 
v2
Comments
Albin Abel 8-Apr-11 11:40am    
Agreed. My 5
"I have to initiate a client application automatically as the server application start. For detecting the server application running or not, i was using the Window Service. But through Window service i am not able to start client application. If server application is not running, client application can not be start even by the user. Can you suggest me what i have to use to initiate client application? Thanks."

You should probably rethink your architecture, this doesn't sound like a good idea to me. A windows service shouldn't be trying to start GUIs based on some remote server application status

I think it's down to the user to decide when they want to run a piece of software. When they start your application, it should perform some sort of check against your server application that returns a 'running \ valid' status if the server app is all OK and ready for clients.

If not, you should see some standard message 'The remote server is not accepting connections, please try again later'.

This is really easy to do, it just depends how your communicating with your server.
 
Share this answer
 
Comments
Oshtri Deka 8-Apr-11 9:56am    
I agree.
Albin Abel 8-Apr-11 11:41am    
Agreed
A service runs even when there's no user logged in, so running a WinForms app generally isn't possible from a service.

You *can* run console apps with no problem, which can check to see if a user is logged on, and then itself spawns a WinForms app, or can run an app with a UI (i.e., your typical windows application) if you set Interacts With Desktop to true for the service.

What I would do is put the winforms app in the Startup folder, and when it starts, minimize it to the system tray. That way, the app is running whenever a user logs in.
 
Share this answer
 
v2
Comments
Kim Togo 8-Apr-11 7:44am    
"Interacts With Desktop" is not possible from Windows Vista / 7 and Windows Server 2008
#realJSOP 8-Apr-11 8:12am    
According to Microsoft it's not possible, but you sorta can get around it.
Oshtri Deka 8-Apr-11 9:57am    
Wanna share how :D?
Albin Abel 8-Apr-11 11:39am    
Agreed. My 5
A typical approach used in Vista/Windows 7 is to have a thin stub client running at startup (via the registry key). This thin client communicates with the windows service and lets it know that a user has logged in, and so the main UI can be started as and when required. The service now detects the server-service and if it's up and running it knows it's safe to launch the main UI, and so it communicates back to the thin client letting it know that the main app should be run now. The thin client then launches the primary executable.

May sound messy, but it's the simplest design possible to avoid hassles involved in directly starting a UI process from a service.
 
Share this answer
 
Comments
Kim Togo 8-Apr-11 8:20am    
Good answer.
Espen Harlinn 8-Apr-11 8:24am    
Reasonable solution, fairly easy to implement - my 5
Nish Nishant 8-Apr-11 8:26am    
Thank you, Espen!
Albin Abel 8-Apr-11 11:38am    
Good advice. My 5
Nish Nishant 8-Apr-11 11:44am    
Thanks Albin!

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