通过互操作获取并释放Mutex

时间:2013-09-26 15:02:39

标签: c# concurrency mutex

我们有一个可以运行多个实例的旧版VB6可执行文件。我们希望某些作业只允许一个并发实例。

似乎OS Mutex非常适合,因为这是一个遗留应用程序,所有新代码必须用C#编写并通过互操作访问。

我创建了一个将获得的类:

public bool AcquireLock(string JobId)
{
    // get application GUID as defined in AssemblyInfo.cs
    string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
    appGuid = appGuid + JobId;

    // unique id for global mutex - Global prefix means it is global to the machine
    string mutexId = string.Format("Global\\{{{0}}}", appGuid);

    bool mutexExists = false;

    var mutex = new Mutex(true, mutexId, out mutexExists);
    var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
    var securitySettings = new MutexSecurity();
    securitySettings.AddAccessRule(allowEveryoneRule);
    mutex.SetAccessControl(securitySettings);

    return mutexExists;
}

释放锁定:

public bool ReleaseLock(string JobId)
{
    // get application GUID as defined in AssemblyInfo.cs
    string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
    appGuid = appGuid + JobId;

    // unique id for global mutex - Global prefix means it is global to the machine
    string mutexId = string.Format("Global\\{{{0}}}", appGuid);

    var mutex = Mutex.OpenExisting(mutexId);

    mutex.ReleaseMutex();

    return true;
}

在尝试释放锁之前,所有似乎都运行良好:

[TestMethod()]
public void ReleaseLockTest()
{
    var target = new MutexConcurrencyHelper();
    var jobId = RandomUtils.RandomString(8, true);
    var expected = true;
    bool actual;
    actual = target.AcquireLock(jobId);
    Assert.AreEqual(expected, actual);

    target.ReleaseLock(jobId);

    var expected1 = true;
    bool actual1;
    actual1 = target.AcquireLock(jobId);
    Assert.AreEqual(expected1, actual1);
}

获得锁的第二次尝试发现锁已经到位。为什么锁定不释放?

2 个答案:

答案 0 :(得分:1)

构造函数上的out值不是您想要返回的值,表示是否已获取互斥锁。它仅指示指定的互斥锁名称是否为新。将initiallyOwned(第一个参数)指定为false,然后指定return mutex.WaitOne();

你可能想让AcquireLock在超时时尝试“尝试获取锁定”。请查看this SO answer以获取完整示例。

答案 1 :(得分:0)

感谢Hans我创建了一个更简单的解决方案,这也使我们能够限制运行的实例数量,这也是可取的:

编辑:为完整性添加了GenerateMutexId。

class SemaphoreConcurrencyHelper: IConcurrencyHelper
{
    private Semaphore _semaphore;

    public bool AcquireLock(string LockId, int Instances)
    {
        try
        {
            _semaphore = Semaphore.OpenExisting(GenerateMutexId(LockId)); //Acquire existing Semaphore (if exists)                   
        }
        catch (WaitHandleCannotBeOpenedException) // Create new Semaphore if not exists
        {
            _semaphore = new Semaphore(Instances, Instances, GenerateMutexId(LockId));                
        }

        return _semaphore.WaitOne(TimeSpan.FromSeconds(10), false); // Block thread until Semaphore has slot available within specified Timespan
    }

    public bool ReleaseLock()
    {
        try
        {
            _semaphore.Release(1); // Increment the count on the Sempahore by 1
        }
        catch (Exception e)
        {
            return false; //TODO: Add an error log
        }
        _semaphore = null;
        return true;
    }

    private string GenerateMutexId(string LockId)
    {
        // Get application GUID as defined in AssemblyInfo.cs
        string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
        appGuid = appGuid + LockId;

        // Unique id for global mutex - Global prefix means it is available system wide
        return string.Format("Global\\{{{0}}}", appGuid);
    }
}

到目前为止,我所有的测试用例都已通过,欢迎评论。

相关问题