C# 显示来自窗口服务的窗口窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12953148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:04:43  来源:igfitidea点击:

show a windows form from a window service

c#winformswindows-services

提问by Arshad

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:

我正在创建一个窗口服务。我的要求是在特定时间间隔显示来自 windows NT 服务的窗口窗体。出于测试目的,我只想在服务启动时显示表单:

 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.

它不工作。既没有显示表单,也没有运行计算过程。我发现了一些显示弹出的链接 ,但其中大多数建议使用 WCF。没有 wcf 是不可能的。任何人都可以告诉我实现这一目标的方法。

回答by CoreTech

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

选中“与桌面交互”框将适用于 Windows NT、2000、XP 和 2003,但由于会话 0 隔离,该设置在 Windows Vista 及更高版本中不再像您期望的那样工作。在开发交互式服务之前,您要非常仔细地考虑......

回答by Justin

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.

做不到*。在以后无法作为 Windows 服务工作的操作系统中,不允许与桌面交互 - 相反,Windows 服务提供的 UI 显示在Session 0 中,这是一个特殊的登录会话,通常对最终用户不可见。

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 startupand have an icon in the notification area) and communicates with the Windows Service using some form of IPC

您应该做的是编写一个单独的 Windows 窗体应用程序,该应用程序始终在运行,但并不总是可见(可能在启动时运行该应用程序并在通知区域中有一个图标)并使用某种形式的IPC与 Windows 服务通信

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.

当 Windows 服务希望向用户显示某些 UI 时,它会向应用程序发送一条消息,该应用程序又会向最终用户显示所需的 UI。

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

**或者至少绝对不应该这样做*

回答by Pradip

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

我只是指 StackOverflow 中另一个链接中给出的答案

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

如何从与桌面交互的应用程序与 Windows 服务进行通信?

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

请注意,如果您计划最终在 Windows Vista 或 Windows Server 2008 上进行部署,那么今天可以完成的许多方法将不起作用。这是因为引入了称为“会话 0 隔离”的新安全功能。

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.

大多数 Windows 服务现在已移至会话 0 中运行,以便将它们与系统的其余部分正确隔离。对此的扩展是,第一个登录系统的用户不再被放置在会话 #0 中,他们被放置在会话 1 中。因此,隔离将破坏在服务和桌面应用程序之间进行某些类型的通信的代码。

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.

在服务和应用程序之间进行通信时,今天编写可在 Vista 和 Server 2008 上运行的代码的最佳方法是使用适当的跨进程 API,如 RPC、命名管道等。 不要使用 SendMessage/PostMessage,因为那样会在会话 0 隔离下失败。

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

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

现在,根据您的要求,您将陷入困境。对于跨平台问题,我不确定是否支持远程处理。您可能不得不下拉并一直回到套接字:http: //msdn.microsoft.com/en-us/library/system.net.sockets.aspx