Opening form from a windows service

  • Thread starter Thread starter Vitaly Zayko
  • Start date Start date
V

Vitaly Zayko

Is it possible to attach a form (C# .NET 2) to windows service and show
it in OnStart event? When I tried to do this in general (new, Show())
way it just didn't do anything nor gave me any errors. I want to use
this form for interaction with the service.
Thanks!
Vit Zayko
 
Vitaly said:
Is it possible to attach a form (C# .NET 2) to windows service and show
it in OnStart event? When I tried to do this in general (new, Show())
way it just didn't do anything nor gave me any errors. I want to use
this form for interaction with the service.

You may be able to do it if you change the service to allow interaction
with the desktop - but it's a really bad idea. Services aren't meant to
work like that - they're meant to be entirely in the background.

Instead, your service should expose some kind of control API, probably
over network sockets (eg exposing a remoting service). You could then
write a *separate* Windows Forms app to control it.

Jon
 
Thanks Jon! Probably I have to do as you said. I was thinking that the
way of direct form using will be easier.
 
"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
| Is it possible to attach a form (C# .NET 2) to windows service and show
| it in OnStart event? When I tried to do this in general (new, Show())
| way it just didn't do anything nor gave me any errors. I want to use
| this form for interaction with the service.
| Thanks!
| Vit Zayko

If you feel the need to show something to a user, you first have to ask
yourself if you really need a windows service. Most of the time the answer
is NO.
1. Services are meant to run in the background even when no interactive user
is logged on, they don't have access to the users desktop and they run in a
restricted users security context, so you shouldn't use any UI elements like
Forms in your service.
2. Your OnStart handler should return within 30 seconds, failed to do so
will flag your service as failed to start.

If you want some kind of user interaction with a service you need a second
windows application that uses one of the many IPC mechanisms available in
..NET or windows. The type of IPC you have to select highly depends on the
level of interaction you need, the most appropriate for bi-directional
exchange in .NET is remoting, if you are using v2.0 you can use the
namedpipe remoting channel. If the exchange is uni-directional (eg. service
status, performance info), you may take a look at System.Management
eventing.

Willy.
 
Back
Top