简体   繁体   English

Windows服务使用EventWaitHandle等待命名事件的问题。

[英]Issue with windows service waiting for a named event, using EventWaitHandle.

I'm currently developing a windows service with c# and .net framework 4.5 to extend the functionality of an existing propietary application, this service blocks on an EventWaitHandleClass ( msdn link ) waiting for a named event signaled from the main application. 我目前正在使用c#和.net Framework 4.5开发Windows服务,以扩展现有专有应用程序的功能,该服务在EventWaitHandleClass( msdn链接 )上阻塞,等待主应用程序发出的命名事件。 Something like this: 像这样:

bool boolWithFalse = false;
string eName = "notification_event";
string usr = Environment.UserDomainName + "\\" + Environment.UserName;
EventWaitHandleSecurity security = new EventWaitHandleSecurity(); //*
EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(usr, EventWaitHandleRights.Synchronize, AccessControlType.Allow);
security.AddAccessRule(rule);
EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.ManualReset, eName, out boolWithFalse, security);

//... some non relevant code lines here

//This is where the it locks waiting for the named event
var w = EventWaitHandle.WaitAny(new[] { handle }, Timeout.Infinite);

*: EventWaitHandleSecurity MSDN *: EventWaitHandleSecurity MSDN

Now this works like a charm if i execute my program as a console application, i can easily catch events from the main application and handle them as i intend, BUT, when i install the application as a service it locks waiting for this same named event but never receive the signal. 现在,如果我将程序作为控制台应用程序执行,这就像一个魅力,我可以轻松地从主应用程序中捕获事件并按我的意图进行处理,但是,当我将应用程序安装为服务时,它将锁定等待相同的命名事件但从未收到信号。 The service is set to run using the NT AUTHORITY\\LOCALSERVICE account, i've already tried using my own account (wich the progam uses while running as a console application) and yet nothing happens. 该服务设置为使用NT AUTHORITY \\ LOCALSERVICE帐户运行,我已经尝试使用自己的帐户(在作为控制台应用程序运行时progam使用),但没有任何反应。

If it helps the application that originates the signal is running under my own account. 如果有帮助,那么发出信号的应用程序将在我自己的帐户下运行。 I appreciate any help as i'm a complete beginner developing desktop applications for windows. 感谢您的帮助,因为我是开发Windows桌面应用程序的完整入门者。

You got lost in the security hoopla. 您迷上了安全帽。 Two problems. 两个问题。 The first one is that somebody has to create the event and somebody else has to open it so both components share the same event object. 第一个是有人必须创建事件, 其他人必须打开事件,以便两个组件共享同一个事件对象。 One of them has to use the EventWaitHandle constructor, the other has to call the EventWaitHandle.OpenExisting() method. 其中一个必须使用EventWaitHandle构造函数,另一个必须调用EventWaitHandle.OpenExisting()方法。 The normal way is for the service to create the event and for the UI program to open it. 通常的方法是让服务创建事件并由UI程序打开事件。

Next problem is the event object visibility. 下一个问题是事件对象的可见性。 Windows implements namespaces for named operating system objects, pretty similar to how you use namespaces in the C# language. Windows为命名的操作系统对象实现名称空间,与您使用C#语言使用名称空间的方式非常相似。 The root namespace is named by the session . 根名称空间由会话命名。 And a service runs in a different session than the user's desktop programs. 服务在与用户桌面程序不同的会话中运行。 To get the desktop session program to see the event created in the service session, you have to use a "global" event name. 若要使桌面会话程序查看在服务会话中创建的事件,您必须使用“全局”事件名称。 Which looks like this: 看起来像这样:

 string eName = "Global\\notification_event";

Do be careful of how you name your globally visible named event. 请注意如何命名全局可见的命名事件。 There's another programmer somewhere someday that thinks that "notification_event" is a good choice for a name. 有一天,某位程序员在某个地方认为“ notification_event”是一个不错的名称选择。 You don't want to meet him. 你不想见他。 A {guid} is a good name, you get one from Tools + Create GUID. {guid}是个好名字,您可以从Tools + Create GUID中获得一个。 It is unique in the known universe, possibly beyond. 它在已知宇宙中是唯一的,甚至可能超越。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM