.NET Mutext.ReleaseMutex和Mutex.Close [英] .NET Mutext.ReleaseMutex and Mutex.Close

查看:488
本文介绍了.NET Mutext.ReleaseMutex和Mutex.Close的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是两者之间的区别是什么?如果我关闭我的应用程序时做Mutext.Close()这一翻译Mutext.Release()的,会是什么副作用?

What's the difference between the two? If I do Mutext.Close() intead of Mutext.Release() when shutting down my app, what would be the side effect?

推荐答案

<一个href="http://msdn.microsoft.com/en-us/library/system.threading.mutex.releasemutex%28v=VS.90%29.aspx">ReleaseMutex是用来让另一个线程来获取互斥锁。如果你已经获得互斥(名为的WaitOne 和获得,或通过构造收购)时,才应调用。 重要说明 ReleaseMutex 如果您还没有获取互斥会抛出异常。

ReleaseMutex is used to allow another thread to obtain the mutex. It should only be called if you have acquired the mutex (called WaitOne and acquired it or acquired through the constructor). Important Note ReleaseMutex will throw an exception if you have not acquired the mutex.

<一个href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.close%28v=VS.90%29.aspx">Close是用来清理已分配的声明互斥对象的资源,不管你曾经阻塞与否,如果您已经获得了互斥体,将其释放(等效于调用 ReleaseMutex 。如果你打算在互斥为整个应用程序锁定(即以此来确保您的应用程序的一个实例),那么我会包装在using语句像的约瑟夫阿尔巴哈利的线程指南(必读)的链接是第二部分,向下滚动到互斥部分看到的例子

Close is used to clean up the resources that have been allocated by declaring the mutex object, whether you ever blocked on it or not, if you have acquired the mutex it will release it (equivalent to calling ReleaseMutex. If you plan to lock on the mutex for the whole application (i.e. using this to ensure a single instance of your app), then I would wrap it in a using statement like the example in Joseph Albahari's threading guide (a must read). The link is to the second section, scroll down to the Mutex part to see the example.

这篇关于.NET Mutext.ReleaseMutex和Mutex.Close的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆