我怎样才能摆脱一个废弃的互斥? [英] How can I get rid of an abandoned mutex?
问题描述
摘要:
有没有办法来清除如果进程互斥的?创造了它是死了
详细内容:
我用一个互斥体,以确保只有一个实例我的应用程序运行的。
I use a mutex to make sure that only one instance of my app runs.
在测试了一些新的代码(做自动更新)(0)我跑Environment.Exit。而在调试模式下运行,我的互斥体被清理的罚款。
While testing out some new code (to do auto updating) I ran Environment.Exit(0). While running in debug mode, my mutex was cleaned up fine.
但是,当我改变了构建以释放,那么互斥退出调用后保持周围,是标记为被遗弃的:
But when I changed the build to be 'Release' then the mutex stays around after the exit call and is marked as abandoned:
我有双重检查以确保没有仍在运行,该互斥附加到处理
I have double checked to make sure that there is not a process still running that this mutex is attached to.
现在每次我跑我的应用程序时,它认为互斥量仍然存在,不会推出我的应用程序。 (这也崩溃,因为它试图将消息发送到应用程序,以显示自己的正在运行实例)
Now every time I run my app, it thinks the mutex is still there and will not launch my app. (It also crashes because it tries to send a message to the "currently running" instance of the app to show itself.)
我试图把这样的互斥体:
I tried to free the mutex like this:
bool createdNew;
string applicationId = "18773:TestStudio";
var singleInstanceMutex = new Mutex(true, applicationId, out createdNew);
singleInstanceMutex.Close(); // Tried ReleaseMutex() too
但当然只是得到它andthen释放它。
but of course that just gets it andthen frees it.
我知道我可以只重启,但我跳了的情况下,这个不断发生在生产更好的修补程序。
I know I could just reboot, but I am hopping for a better fix in case this ever happens in production.
有没有办法来清除一个互斥体,如果创建它的过程是死了没有?
推荐答案
不知道这实际上回答你的问题,但这里是我为此使用的代码:
Don't know if this actually answers your question, but here's the code I use for this purpose:
// Mutex object used to determine if there are multiple instances of this program running.
// Note that this is a reference to a .Net Mutex object, not the Windows mutex itself.
private static Mutex _onlyOneInstanceMutex;
/// <summary>
/// Method to test that there is not another instance of the program already running on this
/// machine, or at least in this Terminal Services session or Windows Vista / Windows 7
/// concurrent sessions session. If there is, a message box-style localized error message is
/// displayed and the value false is returned. This implies that this method should not be
/// used in programs that are run as a Windows service.
///
/// This implementation uses a .Net Mutex object in public storage to prevent it from being
/// garbage-collected. The name of the associated Windows mutex is simply the program name as
/// provided by the caller. Neither the .Net Mutex object nor the Windows mutex are ever
/// explicitly released; they remain in existence, perhaps in an "abandoned" state, until the
/// process that created them terminates.
/// </summary>
/// <returns>false if another instance running, otherwise true</returns>
[SuppressMessage("Microsoft.Reliability", "CA2004:RemoveCallsToGCKeepAlive",
Justification = "Not sure if this is correct or not.")]
public static bool TestOnlyOneInstance(string programName)
{
// Funny construct to prevent the Mutex from being garbage collected
GC.KeepAlive(_onlyOneInstanceMutex);
// Test if we are the first instance, and if so create the Windows mutex, making it
// impossible for subsequent instances to successfully create their mutex
bool firstInstance;
_onlyOneInstanceMutex = new Mutex(false, programName, out firstInstance);
if (firstInstance)
return true;
// Display a (possibly localized) error message, then return
string errorMessage = MLocalizer.GetString("Error1",
"Another instance of this program is already running on this machine.") +
"\n" + MLocalizer.GetString("Error2",
"You cannot run two instances at the same time.") +
"\n" + MLocalizer.GetString("Error3", "Please use the other instance.");
MessageBox.Show(errorMessage, programName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
编辑:
唯一的区别我可以在我的代码中看到的和你们的是,我指定最初拥有的为假的,我对垃圾收集的有趣的事情。
Only differences I can see in my code and yours is that I specify "initially owned" as false, and I have the funny thing about the garbage collection.
呵呵,我宣布互斥静态的。
Oh, and I declare the Mutex static.
这篇关于我怎样才能摆脱一个废弃的互斥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!