3

I was creating a windows service in dot net to send automatic email to my friends. All is working well. Now I have a requirement like to show a pop up which says the mail has sent or something like that. But I need some idea to implement that logic. I think it’s not possible to have UI for windows service. SO I thought of creating another windows application to show the pop up message and call this from the windows service when the mail has sent. This is only the idea and not sure is this correct approach. Please guide me.

Thanks.

JleruOHeP
  • 10,106
  • 3
  • 45
  • 71
Bavya
  • 143
  • 1
  • 3
  • 13

5 Answers5

7

There is no restriction that windows service should not have any GUI. Windows services can have GUI. But if it has, it was shown up in Service 0. It was possible in earlier versions of Windows before Vista. Lets have a detailed discussion on it.
      A logon session is created whenever a user logs in to Windows. Each session has a Session ID. All the windows services will run in Session 0.
      In Session 0, a user can see and interact with the graphical elements of any program running there, including those created by Windows Services in earlier versions like Windows NT, 2000, XP and Server 2003.
       Later due to security concerns, Microsoft isolated the Session 0. And the impact is services cannot show their GUI. This is one of the case.
       Consider, A service attempts to create a user interface (UI), such as a dialog box, in Session 0. Because the user is not running in Session 0 due to the isolation of service 0, he or she never sees the UI and therefore cannot provide the input that the service is looking for. The service appears to stop functioning because it is waiting for a user response that does not occur.
      Due to this isolation, services cannot communicate with the applications through Windows message and vice versa. We use RPC, Interactive Services Detection Service (ISDS) and IPC for the communication between services and applications.

Arun
  • 171
  • 5
3

I think it’s not possible to have UI for windows service.

That is partially correct:

Microsoft Windows services [..] do not show any user interface.

See @Arun's answer.

So you'll need a process with a GUI that does the interfacing with the user. To let those two applications (the service and the GUI) communicate, you'll have to look into inter-process communication (IPC).

See this question for an overview of IPC mechanisms usable in C#, but generally advised is to use WCF.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
2

IMO easiest way would be to create another Windows app that will just sit in the tray and wait notifications from windows service. For communication you can use WCF. I have similar solution and it works very well.

See this example of interprocess communication :

http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
1

See "Can a Windows Service have a GUI" in the Windows Services FAQ for a discussion on this topic.

Technically the WTSSendMessage function leaves the door open (even on modern versions of windows) but you will be much better off designing a "headless" service application and relying on another GUI component to interact with the user, as others have suggested.

CoreTech
  • 2,345
  • 2
  • 17
  • 24
-1

It's possible to show a pop box. You can add GUI to windows services by adding the namespace System.Windows.Forms. Before that right click on the references tab present in the solution explorer and add this namespace.

Now you can use any gui control in this. For example:

MessageBox.Show("mailed succesfuly");
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
  • 1
    Sorry to vote down, but your answer is incorrect. Windows Vista and after use Session 0 Isolation - effectively showing any UI of the service in a dead space which could lead to the service becoming unresponsive. Please see http://msdn.microsoft.com/en-us/library/windows/hardware/dn653293(v=vs.85).aspx for more info, – shadowf Dec 19 '14 at 07:07