带有服务和控制台的 System.Threading.Mutex

发布于 2024-12-08 08:48:22 字数 570 浏览 0 评论 0原文

我的程序作为服务或控制台运行。我创建了一个 System.Threading.Mutex 来仅启动两种模式之一。这里我的代码

public class MyServer 
{
// Private variable
System.Threading.Mutex serverMutex;

public MyServer ()
{
    bool createdNew;
    ServerMutex = new System.Threading.Mutex(true, "MyServerMutex", out createdNew);
    if (!createdNew)
    {
        throw new Exception("Another server instance is running");
    }
}
}

首先启动控制台,然后启动另一个控制台,抛出异常。 首先启动服务和控制台不会引发异常。首先启动控制台,然后启动服务,这是相同的。

有什么想法吗? 我想知道,因为我认为,几个月前当我实现该功能时,它就已经可以工作了。

我正在使用.NET 4.0。

谢谢, 弗雷迪

my program runs as a service or with console. I have created a System.Threading.Mutex to start only one of the two modes. Here my code

public class MyServer 
{
// Private variable
System.Threading.Mutex serverMutex;

public MyServer ()
{
    bool createdNew;
    ServerMutex = new System.Threading.Mutex(true, "MyServerMutex", out createdNew);
    if (!createdNew)
    {
        throw new Exception("Another server instance is running");
    }
}
}

Starting first the console and then another console the exception is thrown.
Starting first the service and the a console doesn't throw the exception. Starting first the console and then the service is the same.

Any ideas?
I'm wondering because I think, that it has allready work some months ago, when I implemented the feature.

I'm using .NET 4.0.

Thanks,
Freddy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2024-12-15 08:48:22

问题可能是因为您的服务在不同的凭据下运行,因此互斥体不在用户/会话之间“共享”...
您必须在互斥锁的名称前添加“Global\”
看看这个好帖子< /a>.

Probably problem is because your service runs under different credentials, so mutex is not "shared" between users/sessions...
You have to prefix the name of the mutex with "Global\"
Look at this good post.

紫瑟鸿黎 2024-12-15 08:48:22

正如 Marco 所说,你的互斥体应该以类似 : 的方式命名

string.Format(@"Global\{0}", mutexName);

,以出现在全局命名空间中(对所有登录用户可见) 。如果您需要操作由服务创建的互斥锁,您还需要指定谁可以操作它:

var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);

var security = new MutexSecurity();
security.AddAccessRule(new MutexAccessRule(users, MutexRights.Synchronize, AccessControlType.Allow));
security.AddAccessRule(new MutexAccessRule(users, MutexRights.Modify, AccessControlType.Allow));

new Mutex(false, name, out createdNew, security);

As Marco said your mutex should be named with something like :

string.Format(@"Global\{0}", mutexName);

to appear in the global namespace (visible to all logged in users). If you need to manipulate the mutex once created by the service you will also need to specify who could manipulate it :

var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);

var security = new MutexSecurity();
security.AddAccessRule(new MutexAccessRule(users, MutexRights.Synchronize, AccessControlType.Allow));
security.AddAccessRule(new MutexAccessRule(users, MutexRights.Modify, AccessControlType.Allow));

new Mutex(false, name, out createdNew, security);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文