How to show a form in windows service.

13,911

Solution 1

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.

Solution 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.

Share:
13,911
Farna
Author by

Farna

Life is love & Life is knowledge.

Updated on June 06, 2022

Comments

  • Farna
    Farna almost 2 years

    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();
    }