转移 .NET 互斥锁的所有权

发布于 2024-12-10 19:21:33 字数 614 浏览 0 评论 0原文

我读过有关互斥锁由线程拥有并且只能由拥有线程使用的信息。在 这个答案解决方案表明,每个进程必须在向另一个进程发出已通过信号之前获得互斥锁的所有权。我必须在这里承认我的愚蠢,我不知道如何使用 IPC 事件,因为它们没有出现在我阅读的 MSDN 中,我喜欢唯一命名的 mutex 作为我的解决方案但我什至不知道如何在 WindowsService 和常规进程之间转移所有权。请帮忙。

我可以补充一下,Jon Skeet 的教程 此处 告诉我,要在不同的用户,正如我认为的 LocalSystem 一样,需要在互斥体名称前加上“Global\”前缀。我在 .NET 文档中找不到任何提及这一点的内容,我认为他是对的,我必须更多地关注 MSDN 系统之外的内容。

I've read about mutex's being owned by threads and only usable by the owning thread. In this answer the solution suggests that each process must take ownership of the mutex before signalling the other that is is through. I must admit my stupidity here, I don't know how I would use events for IPC as they haven't popped up in my reading of MSDN, I like the uniquely named mutex's as my solution but I don't even know how to transfer ownership between the WindowsService and regular process. Please help.

Can I add, Jon Skeet's tutorial here informs me that to communicate between different users, as I suppose LocalSystem to be, requires prefixing mutex names with 'Global\'. I could not find any mention of this in the .NET documentation, I suppose he is right and must I look outside of the MSDN system more than I do.

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

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

发布评论

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

评论(3

小霸王臭丫头 2024-12-17 19:21:33

要获得现有互斥锁的所有权,您可以使用静态方法 Mutex.OpenExisting() 和 MutexRights = TakeOwnership

public static Mutex OpenExisting(
                                string name,    
                                MutexRights rights)
  • 权限参数必须包含 MutexRights.Synchronize 标志以允许线程等待互斥锁,并包含 MutexRights.Modify 标志以允许他们调用 ReleaseMutex() 方法。
  • OpenExisting 方法尝试打开现有的命名互斥体。如果系统互斥体不存在,则此方法将引发异常而不是创建系统对象。

考虑到 OpenExisting() 可能会抛出 UnauthorizedAccessException 异常,我建议使用以下构造函数创建一个新的互斥体:

public Mutex(
    bool initiallyOwned,
    string name,
    out bool createdNew
)

然后通过分析 createdNew 变量的值,您可以检查互斥体是否已存在或是否创建了新的互斥体。

To take ownership on already existing Mutex you can use static method Mutex.OpenExisting() with MutexRights = TakeOwnership

public static Mutex OpenExisting(
                                string name,    
                                MutexRights rights)
  • The rights parameter must include the MutexRights.Synchronize flag to allow threads to wait on the mutex and the MutexRights.Modify flag to allow them to call the ReleaseMutex() method.
  • The OpenExisting method attempts to open an existing named mutex. If the system mutex does not exist, this method throws an exception instead of creating the system object.

Considering that OpenExisting() can throw UnauthorizedAccessException exception I would suggest to create a new Mutex using following constructor:

public Mutex(
    bool initiallyOwned,
    string name,
    out bool createdNew
)

and then by analyzing a value of the createdNew variable you can check whether mutext already exists or new one was created.

浮生未歇 2024-12-17 19:21:33

第一个过程:

// Create a new Mutex. 
// True means this thread/process has inital ownership
Mutex mut = new Mutex(true, "some unique name");
//Now do your job
//Release ownership
mut.ReleaseMutex();

现在是第二个过程的代码。

// Create an instance of mutex object with the same name
// This means this thread/process has NOT inital ownership
Mutex mut = Mutex.OpenExisting("some unique name");
//Your thread will block until the first passes ownership
mut.WaitOne();
//Now you have it and can do your job

First process:

// Create a new Mutex. 
// True means this thread/process has inital ownership
Mutex mut = new Mutex(true, "some unique name");
//Now do your job
//Release ownership
mut.ReleaseMutex();

Now the code for second process.

// Create an instance of mutex object with the same name
// This means this thread/process has NOT inital ownership
Mutex mut = Mutex.OpenExisting("some unique name");
//Your thread will block until the first passes ownership
mut.WaitOne();
//Now you have it and can do your job
只是偏爱你 2024-12-17 19:21:33

页面似乎相当完整地回答了我的问题,这里尝尝:

好吧,在成功完成 mutex.WaitOne() 后,当前线程就是互斥锁的所有者。然后,在调用 mutex.ReleaseMutex() 后,线程不再拥有该互斥锁。

This page seems to have answered my question fairly completely, here's a taste:

Well, after a successful mutex.WaitOne() has completed the current thread is the owner of mutex. Then after a call to mutex.ReleaseMutex() the thread does not own the mutex any more.

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