UI for windows service in dot net

倖福魔咒の 提交于 2020-01-24 13:45:27

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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");


来源:https://stackoverflow.com/questions/14067199/ui-for-windows-service-in-dot-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!