相关疑难解决方法(0)

创建互斥锁会抛出DirectoryNotFoundException

我正在尝试创建一个命名的互斥锁,但是当我调用构造函数时,我得到了一个DirectoryNotFoundException!为什么互斥锁试图访问文件系统,我怎么知道什么是有效路径?是否应该放置互斥锁的特定目录,以及它与名称的对应关系如何?

编辑:我正在使用Mutex(bool, string)重载,例外是:

System.IO.DirectoryNotFoundException: Could not find a part of the path '<mutex name>'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.Threading.Mutex.<>c__DisplayClass3.<.ctor>b__0(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& createdNew, MutexSecurity mutexSecurity)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name)
Run Code Online (Sandbox Code Playgroud)

.net mutex

16
推荐指数
1
解决办法
2093
查看次数

在 Windows 服务中创建时找不到命名事件

我正在用 C# 开发一个 Windows 服务来集中管理一些应用程序连接。它通常是一个休眠服务,它在被外部可执行文件唤醒时执行一些操作。为此,我使用了命名事件,特别是 .NET EventWaitHandle. 我的代码在服务端归结为:

        EventWaitHandleSecurity sec = new EventWaitHandleSecurity();
        sec.AddAccessRule(new EventWaitHandleAccessRule(
                  new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                  EventWaitHandleRights.FullControl,
                  AccessControlType.Allow));
        evh = new EventWaitHandle(false, EventResetMode.AutoReset, EVENT_NAME, 
                                  out created, sec);

        Log(created ? "Event created" : "Event already existed?");
Run Code Online (Sandbox Code Playgroud)

由于它是受信任服务器上的内部应用程序,因此我不介意将“完全控制”授予“世界”一般不明智。

在客户端,我有:

EventWaitHandle.TryOpenExisting(EVENT_NAME, EventWaitHandleRights.Modify, out evh)
Run Code Online (Sandbox Code Playgroud)

当我在基于控制台的交互模式下运行我的服务时,上面的代码可以完美运行。两端都找到事件,客户端可以设置,服务开始工作。大家都很开心。

但是在安装服务时它不起作用。日志记录仍然报告该事件是重新创建的,但客户端找不到该事件。因为我认为它与安全相关,所以我添加了 World Full Control Allow 访问规则,但它没有改变任何东西。我将服务更改为以本地管理员身份运行,即使以我自己的用户帐户身份运行,但什么也没有 - 即使日志显示该服务正在愉快地对其进行轮询,客户端也找不到该事件。如果我更改TryOpenExistingOpenExisting我得到一个明确的异常:

System.Threading.WaitHandleCannotBeOpenedException: No handle of the given name exists.
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

.net c# winapi synchronization inter-process-communicat

4
推荐指数
1
解决办法
923
查看次数