C# 使用 waitone() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/815011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using waitone() method
提问by GurdeepS
static Mutex mutex = new Mutex (false, "oreilly.com OneAtATimeDemo");
static void Main()
{
// Wait a few seconds if contended, in case another instance
// of the program is still in the process of shutting down.
if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))
{
Console.WriteLine ("Another instance of the app is running. Bye!");
return;
}
try
{
Console.WriteLine ("Running. Press Enter to exit");
Console.ReadLine();
}
finally { mutex.ReleaseMutex(); }
}
http://www.albahari.com/nutshell/ch20.aspx
http://www.albahari.com/nutshell/ch20.aspx
In this code:
在这段代码中:
if(mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
mutex.ReleaseMutex();
}
else
{
MessageBox.Show("only one instance at a time");
}
http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
There is no inversion of the if/bool.
if/bool 没有反转。
If waitone() is true, does that mean an instance is already running? If true is returned, the current thread will be blocked which would mean two processes calling the same app will both halt?
如果 waitone() 为 true,是否意味着实例已经在运行?如果返回 true,当前线程将被阻塞,这意味着调用同一个应用程序的两个进程都将停止?
My understanding is as follows:
我的理解如下:
// Don't block the thread while executing code.
//Let the code finish and then signal for another process to enter
What is the implication of no ! (returning true) and vice versa. Or in other words, what happens either way?
没有的含义是什么!(返回真),反之亦然。或者换句话说,这两种方式会发生什么?
I know waitAll for example, waits for all threads to finish. Jon Skeet showed a good example of this on his site, which has stuck in my mind (credit to his explanations). So obviously waitOne waits for one thread to finish. The return value is what confuses me.
例如,我知道 waitAll 等待所有线程完成。Jon Skeet 在他的网站上展示了一个很好的例子,这让我印象深刻(归功于他的解释)。所以很明显 waitOne 等待一个线程完成。返回值让我感到困惑。
采纳答案by Lasse V. Karlsen
To wait on a mutex means to wait until you can acquire it.
等待互斥锁意味着等待,直到您可以获得它。
WaitOne on a Mutex will return true
if the mutex could be acquired in the given time. If it couldn't, the method will return false
. If the mutex was acquired, it's your responsibility to release the mutex when you're done with it.
true
如果可以在给定时间内获取互斥锁,则互斥锁上的 WaitOne 将返回。如果不能,该方法将返回false
。如果获得了互斥锁,则您有责任在完成后释放互斥锁。
If you can acquire a named mutex, nobody else owns it at the moment.
如果您可以获得一个命名的互斥锁,那么目前没有其他人拥有它。
So, to recap. If you can acquire the mutex, the method returns true
and you're the first/only instance of your application, in regards to your question.
所以,回顾一下。如果您可以获得互斥锁,则该方法将返回,true
并且您是应用程序的第一个/唯一实例,就您的问题而言。
If you cannot acquire the mutex, the method returns false
and there is another application instance currently owning the mutex by that name.
如果您无法获取互斥锁,该方法将返回,false
并且当前有另一个应用程序实例拥有该名称的互斥锁。
回答by Timotei
In the MSDN the description of mutex.WaitOne() is this:
在 MSDN 中 mutex.WaitOne() 的描述是这样的:
Blocks the current thread until the current WaitHandle receives a signal.
阻塞当前线程,直到当前 WaitHandle 接收到信号。
And the return value is: true if the current instance receives a signal
并且返回值为:true 如果当前实例收到信号
So until the application is started once, it won't receive any signal from the mute.WaitOne() method. The thread is blocked like it says in the description.
因此,在应用程序启动一次之前,它不会收到来自 mute.WaitOne() 方法的任何信号。线程被阻塞,就像描述中所说的那样。
So to answer you question:
所以回答你的问题:
What is the implication of no ! (returning true) and vice versa.
没有的含义是什么!(返回真),反之亦然。
If you negate the mutex.WaitOne() method, then you will check this: If NOT waitOne(), it means you check if the waitOne doesn't respond with true.
如果您否定 mutex.WaitOne() 方法,那么您将检查:如果不是 waitOne(),则表示您检查 waitOne 是否没有响应true。
I hope this is more clear now.
我希望现在更清楚了。