6

I want to load a form in OnStart() method in my windows service; here is my code. It is not working. Can you please provide any help ?

protected override void OnStart(string[] args)
{
    Form1 fr = new Form1();
    fr.Show();
}
Haider Ali Wajihi
  • 2,756
  • 7
  • 51
  • 82
Farna
  • 1,157
  • 6
  • 26
  • 45
  • 3
    you don't load/show forms in a service. That's the point! – Mitch Wheat Mar 06 '11 at 15:14
  • Why would you try and do such thing? – PedroC88 Mar 06 '11 at 15:25
  • @PedroC88:i want to get username and time of kogin and logout of any users,and i write my code in the form1_load and form1_closing and then i load my form with my service ,but it dosn't work at all. – Farna Mar 06 '11 at 15:33
  • Please note that people won't login/logout of the service. They Will login and out of an application consuming the service. Such application is what should provide the data. The service should limit itself to receive it, process it and perhaps store it. – PedroC88 Mar 06 '11 at 15:36
  • @PedroC88:can i use Task Scheduler for this???i'm new programmer,can i run my Task Scheduler when the user login and logout??? – Farna Mar 06 '11 at 16:08
  • What is it that you are trying to accomplish? Perhaps you got an issue with the approach rather than the implementation. – PedroC88 Mar 06 '11 at 16:15
  • @PedroC88:my first language is persian,i explain my goal,is not clear?? – Farna Mar 06 '11 at 16:20
  • When you said you wanted to get the time and user that logs in... did you mean that log into windows? – PedroC88 Mar 06 '11 at 16:31
  • Then you should try and look into Windows auditing... I dunno much about the subject but I think that would be the way to go. – PedroC88 Mar 06 '11 at 18:35

4 Answers4

5

You can't use services that way. Services can't interact with desktop directly, because they run in another WindowsStation from the logged in users session. You need to create another application that will communicate with your service.

How to make communication you can read on MSDN and in this example. Some ideas also described already on StackOverflow.

Community
  • 1
  • 1
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
2

Services run in a different window station and desktop to any interactive user. Even if the form is loaded successfully nobody will be able to see it.

You can set the "Allow service to interact with desktop" service option which allows a service to share the console's window station. However, this is a really bad idea. It opens up security holes and a host of other problems. E.g. what happens if there is more than one user logged in? Or if you're running terminal services?

A more conventional design is to have a client application handling the UI and talking to the service running in the background.

arx
  • 16,686
  • 2
  • 44
  • 61
0

GUI requires Single-Threaded Apartment threading model. Forms require a message pump (like the one started by Application.Run).

A service is definitely not designed to show GUI (even interactive services are considered bad practice), it can be controlled from a GUI, though.

Jaroslav Jandek
  • 9,463
  • 1
  • 28
  • 30
0

For a service to display a window it has to be marked as "Allow interaction with desktop". This can be done by the service installer or on the property page for that service.

That's not enough to get the window to display reliably, though. In practice you will have to determine if there is a user currently logged in and get their desktop. This is not a trivial undertaking and can be a source of security issues. If there is no one currently logged in, you are out of luck.

The best solution is to have a separate GUI app which talks to the service via some IPC mechanism.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299