我应该处置互斥锁吗?

发布于 2024-11-30 08:56:25 字数 290 浏览 2 评论 0原文

我正在开发 2 个 Windows 服务,它们有一个公共数据库,我想用系统互斥体锁定(跨进程)该数据库。

现在我想知道是否可以在 try-finally 块中调用 WaitOne()ReleaseMutex() 还是我也应该处理互斥体(例如在 using 块中)。如果是这样,我想我应该总是在 WaitOne() 方法上捕获 AbandonedMutexException ,还是我错了?

I'm working on 2 Windows Services that have a common database which I want to lock (cross-process) with a system Mutex.

Now I'm wondering whether it's ok to just call WaitOne() and ReleaseMutex() in a try-finally block or should I also dispose the Mutex (e.g. in a using block). If so I guess I should always catch the AbandonedMutexException on the WaitOne() method or am I wrong here?

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

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

发布评论

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

评论(3

梦归所梦 2024-12-07 08:56:25

互斥锁是一个 Windows 内核对象(此处包装在 .NET 对象中)。

因此,它是一种应该被处置的非托管资源。

更准确地说,.NET 对象包含互斥锁的句柄,必须以某种方式释放/处置该互斥锁。

我不相信 Mutex 类文档中的代码示例 互斥对象未被释放。尽管 Henzi 在评论中提出了一个很好的观点:互斥对象是静态的,当进程退出时,它要么被终结器处理,要么被 Windows 内核销毁。

另请注意 关闭() 也处理该对象。

当然,即使您不使用现有的互斥对象,将其保留在应用程序中也没有什么问题。它们是轻资源。

A mutex is a Windows kernel object (here wrapped in a .NET object).

As such, it is an unmanaged resource that should be disposed.

More accurately, the .NET object contains a HANDLE to the mutex, which must be released/disposed of somehow.

I don't trust that code sample in the Mutex class docs where the mutex object is not disposed. Although Henzi has a good point in comment: The Mutex object is static and would be either disposed by the finalizer or destroyed by the Windows kernel when the process exits.

Also, note that Close() disposes the object as well.

Of course, there's nothing wrong with keeping an existing Mutex object in your app even while you don't use it. They are light resources.

毁梦 2024-12-07 08:56:25

根据 this 一个名为 Mutex 会在以下情况下自动销毁:最后一个持有该互斥体HANDLE的进程结束。

在非托管术语 MSDN 中说

使用CloseHandle函数关闭句柄。当进程终止时系统自动关闭句柄。当互斥对象的最后一个句柄关闭时,该对象将被销毁。

在 .NET 中,您应该调用 .Close() Mutex 上的 - 这会释放 HANDLE...因为每个进程在访问时都会获得自己的 HANDLE同名Mutex 这是一致的做法...一旦进程不再存在(终结器和所有),不调用 Close() 不会留下任何问题...

According to this a named Mutex is automatically destroyed when the last process holding a HANDLE of that Mutex ends.

In non-managed terms MSDN says

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.

In .NET you should call .Close() on the Mutex - this releases the HANDLE... since every process gets its own HANDLE when accessing even the same named Mutex this is consistent practice... not calling Close() won't leave any problems behing once the process is no more (finalizers and all)...

酷炫老祖宗 2024-12-07 08:56:25

您需要处置等待句柄使用的资源。

从文档中:

释放WaitHandle当前实例使用的所有资源
班级。 (继承自WaitHandle。)

waithandle 使用非托管资源,应在使用结束时将其释放。

MSDN 文档互斥体

You need to dispose the resources which are used by the waithandle.

From the documentation:

Releases all resources used by the current instance of the WaitHandle
class. (Inherited from WaitHandle.)

The waithandle uses unmanaged resources which should be disposed at the end of use.

MSDN Documentation Mutex

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