如果您不能为Mutex命名,它有什么用? [英] What is the use for a Mutex if you can't name it?

查看:128
本文介绍了如果您不能为Mutex命名,它有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难确定互斥量有多好,而又没有命名.具体来说,我想使Windows Mobile 6.5应用程序成为单实例.

I am having a hard time figuring out what good a mutex is without naming it. Specifically I want to make my Windows Mobile 6.5 app single-instance.

此网站上有一些有关如何执行此操作的问题和解答-最好的问题似乎是使用命名的互斥体.

There are a few questions and answers on this site about how to do that - and the best ones seem to use named mutexes.

不幸的是,紧凑框架中互斥锁的CTORS不要使用字符串-只能创建一个互斥体.

Unfortunately the CTORS for mutexes in compact framework do not take a string - one can only create a mutex.

现在,如果互斥锁没有关联的ID,那有什么用?

Now, what good is a mutex if it has no associated ID?

我想念什么吗?

如果无法为多个应用程序命名,则如何使用互斥体来保护资源?

How do I use a mutex to protect a resource across multiple applications if I can't name them?

推荐答案

在紧凑型框架本身中,没有.

In the compact framework itself, no there is not.

如果您不能为Mutex命名,它有什么用?

What is the use for a Mutex if you can't name it?

未命名的互斥体被称为本地互斥体.您仍然可以使用它在同一进程中的不同线程之间进行同步.诸如lock关键字的监视器没有相同数量的功能.正如 ctacke 所述,互斥锁不允许递归入口.另外,不能跨AppDomain边界使用监视器.此外,互斥体可以与诸如 WaitHandle的有用的东西一起使用. .WaitAll WaitAny,其中监视器不能与任何这些东西一起使用.

An unnamed mutex is said to be a local mutex. You can still use it for synchronizing between different threads in the same process. A monitor like the lock keyword doesn't have the same amount of functionality. As ctacke notes, a Mutex does not allow recursive entrance. Also, a monitor cannot be used across AppDomain boundaries. Additionally, a Mutex can be used with helpful things like WaitHandle.WaitAll or WaitAny, where a monitor cannot be used with any of those things.

我想念什么吗?

Am I missing something?

否-在.NET CF Framework中,如果没有平台调用的帮助,则无法命名互斥体.

No - In the .NET CF Framework you cannot name a mutex without the help of platform invoke.

如果无法为多个应用程序命名,则如何使用互斥体来保护资源?

How do I use a mutex to protect a resource across multiple applications if I can't name them?

您必须命名它们-并且可以做到这一点,您只需要诉诸某种平台调用即可.基于Windows CE的系统暂时支持 称为互斥体.您可以编写一个自己执行的P/Invoke调用:

You have to name them - and you can do that, you just have to resort to some platform invoke. Windows CE based system have supported named mutexes for a while. You could write a P/Invoke call that does it yourself:

[DllImport("Coredll.dll")]
public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool initialOwner, string lpName);

并将其用作(例如)CreateMutex(IntPtr.Zero, false, "MutexName");

您还必须为 ReleaseMutex CloseHandle .

You would also have to write P/Invoke calls for ReleaseMutex and CloseHandle.

特别是我想使Windows Mobile 6.5应用程序成为单实例.

Specifically I want to make my Windows Mobile 6.5 app single-instance.

命名互斥锁是一种解决方案.另一种可能对您有用的解决方案是使用文件锁.

A named mutex is one solution. Another solution that may work for you is to use a file lock.

  1. 如果不存在,请在启动时创建一个名为foo.txt的文件.
  2. 使用 FileShare.None .
  3. 不同的实例将无法获得对其的写锁定,因为第一个实例对其具有锁定并且不会共享它.捕获异常并终止程序,因为其中一个已在运行.
  4. 当程序关闭时,请清理锁.即使进程崩溃或异常终止,也应删除文件上的锁,以允许另一个实例启动.像这样:

  1. Create a file named foo.txt on start up if it doesn't exist.
  2. Acquire a write lock on the file file using FileShare.None.
  3. A different instance will not be able to aquire a write lock on it since the first instance has a lock on it and won't share it. Catch the exception and terminate the program since one is already running.
  4. When the program closes, clean up the lock. Even if the process crashes or terminates abnormally, the lock on the file should be removed allowing another instance to start. Something like this:

FileStream stream;
try
{
     stream = new FileStream("lock.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
}
catch
{
    MessageBox.Show("Program is already running.");
    return;
}
//Put your application code here.
MessageBox.Show("Program is now running.");
stream.Close();

这篇关于如果您不能为Mutex命名,它有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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