3

i am creating a window service. my requirement is to display window form from window NT service on particular interval. For testing purpose , i just want to display the form on service start:

 protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart -before form show");

            Messager_Form obj = new Messager_Form();
            obj.Show();
           // System.Diagnostics.Process.Start("calc.exe");
            eventLog1.WriteEntry("In OnStart -after form show");
           // timer1.Start();
        }

it not working. Neither form is showing nor calc process is running. i have found some links showing pop up , but most of them suggesting WCF. isn't it possible without wcf. can anybody show me the way to achieve this.

Community
  • 1
  • 1
Arshad
  • 9,454
  • 6
  • 35
  • 60
  • 3
    You shouldn't show windows from a service. Services are meant to run without interaction. – Daniel Hilgarth Oct 18 '12 at 11:05
  • 1
    You should mark your service as `Interact with desktop` – L.B Oct 18 '12 at 11:05
  • Consider writing messages to Event Log. Use configuration file for enabling/disabling logging to Event Log. – jags Oct 18 '12 at 11:06
  • can't i run a separate process from service ? – Arshad Oct 18 '12 at 11:08
  • 5
    Open a named pipe, have a client (non-service) program running in background and watch the pipe, that's it. – Alvin Wong Oct 18 '12 at 11:08
  • @ Alvin Wong: how can i do this. i am new to window service. can you share me useful links regarding that. – Arshad Oct 18 '12 at 11:13
  • @Arshad eg http://www.switchonthecode.com/tutorials/interprocess-communication-using-named-pipes-in-csharp – stuartd Oct 18 '12 at 11:27
  • If you want to interact with your service, write a winform/console application that communicates with the NT Service. NT Services run without anyone logged in (i.e. there is no desktop to show the window on!). – Marvin Smit Oct 18 '12 at 11:41
  • @Marvin Smit : my requirement is to show windows form from windows sevice . – Arshad Oct 18 '12 at 11:45
  • When you say "Windows Service", do you mean a Windows NT Service or a Windows hosted Web Service? – Marvin Smit Oct 18 '12 at 12:40
  • Window NT service.. running on the local computer. – Arshad Oct 18 '12 at 12:43
  • @Arshad - you need to revisit this requirement - it's been made more and more difficult to do this with recent versions of Windows - it's just not the right approach. – Will Dean Oct 18 '12 at 12:47
  • OK, approaching it from a slightly different angle, if it needs interaction - why does it need to be a service? – BugFinder Oct 18 '12 at 12:48
  • @ BugFinder- i want to run some wmi query on particular interval and i want to show the user. – Arshad Oct 18 '12 at 12:52
  • http://stackoverflow.com/questions/267838/how-can-a-windows-service-execute-a-gui-application – Matt Davis Oct 18 '12 at 13:05
  • I have used this code project article to launch a app in session1 from a session0 service, works like a charm. http://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-32-and-64-bit-Archite – Bali C Oct 18 '12 at 13:51
  • did you tried any final solution with full source code sample in github using good patterns and practices? – Kiquenet Apr 06 '18 at 23:06

3 Answers3

6

Cant be done*. In later Operating Systems that won't work as Windows Services are disallowed from interacting with the Desktop - instead UI presented by Windows Services is shown in Session 0, a special logon session which is not normally visible to the end user.

What you should instead do is write a separate Windows Forms application which is always running, but not always visible (possibly have that application run at startup and have an icon in the notification area) and communicates with the Windows Service using some form of IPC

When the Windows Service wishes to display some UI to the user it sends a message to the application, which in turns shows the desired UI to the end user.

**or at least it definitely shouldn't be done*

Community
  • 1
  • 1
Justin
  • 82,493
  • 48
  • 216
  • 356
  • 1
    God, Windows is horrible now. If you do this, you might as well just make a freaking Forms application that is always running and ditch the service altogether. – uSeRnAmEhAhAhAhAhA Nov 15 '13 at 09:51
  • 3
    @Tommy Except it would only be running if someone was logged in. You need both the service (to do the heavy lifting even if nobody is logged in), and the desktop application to do notifications to whoever is logged in. It makes more sense this way, especially when you consider multipe sessions - if multiple users are logged in then which user should be shown the UI from the service? – Justin Nov 15 '13 at 10:16
  • The service could send message to GUI application (or viceversa) using WCF. WCF is most recent version of IPC. – Fer Jan 15 '16 at 07:35
2

I am just referring to the answer given in another link in StackOverflow

How to communicate with a windows service from an application that interacts with the desktop?

Answer is :

Be aware that if you are planning to eventually deploy on Windows Vista or Windows Server 2008, many ways that this can be done today will not work. This is because of the introduction of a new security feature called "Session 0 Isolation".

Most windows services have been moved to run in Session 0 now in order to properly isolate them from the rest of the system. An extension of this is that the first user to login to the system no longer is placed in Session #0, they are placed in Session 1. And hence, the isolation will break code that does certain types of communication between services and desktop applications.

The best way to write code today that will work on Vista and Server 2008 going forward when doing communication between services and applications is to use a proper cross-process API like RPC, Named Pipes, etc. Do not use SendMessage/PostMessage as that will fail under Session 0 Isolation.

http://www.microsoft.com/whdc/system/vista/services.mspx

Now, given your requirements, you are going to be in a bit of a pickle. For the cross-platform concerns, I'm not sure if Remoting would be supported. You may have to drop down and go all the way back to sockets: http://msdn.microsoft.com/en-us/library/system.net.sockets.aspx

Community
  • 1
  • 1
Pradip
  • 1,477
  • 11
  • 27
1

Checking the "Interact with desktop" box will work on Windows NT, 2000, XP and 2003 but thanks to Session 0 Isolation that setting no longer works as you may expect in Windows Vista and beyond. You want to think very carefully before developing an interactive service...

CoreTech
  • 2,270
  • 2
  • 17
  • 24