CSharpTest.Net
MutexLock Class
Members  Example  See Also  Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Synchronization Namespace : MutexLock Class

Glossary Item Box

Creates a lock on a mutex that can be released via the Dispose() method, for use in a using statement

Syntax

Visual Basic (Declaration) 
Public Class MutexLock 
C# 
public class MutexLock 

Example

Library/Library.Test/LockingTests/TestMutexLock.cs

C#Copy Code
using (Mutex mtx = new Mutex())
{
    Thread t = new Thread(
        delegate()
        { GC.KeepAlive(new MutexLock(1, mtx)); }
        );
    t.Start();
    t.Join();

    //So the previous thread abandoned the mutex...
    using (MutexLock lck = new MutexLock(mtx))
    {
        Assert.IsTrue(lck.WasAbandonded);
        Assert.AreEqual(mtx, lck.MutexHandle);
    }
}
VB.NETCopy Code
Using mtx As New Mutex()
    Dim t As New Thread(Function() Do
        GC.KeepAlive(New MutexLock(1, mtx))
    End Function)
    t.Start()
    t.Join()

    'So the previous thread abandoned the mutex...
    Using lck As New MutexLock(mtx)
        Assert.IsTrue(lck.WasAbandonded)
        Assert.AreEqual(mtx, lck.MutexHandle)
    End Using
End Using

Inheritance Hierarchy

System.Object
   CSharpTest.Net.Synchronization.MutexLock

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys