Line | |
---|
1 | // -----------------------------------------------------------------------
|
---|
2 | // <copyright file="CallbackLock.cs" company="Microsoft">
|
---|
3 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
4 | // </copyright>
|
---|
5 | // -----------------------------------------------------------------------
|
---|
6 |
|
---|
7 | namespace Microsoft.Kinect.Toolkit
|
---|
8 | {
|
---|
9 | using System;
|
---|
10 | using System.Diagnostics.CodeAnalysis;
|
---|
11 | using System.Threading;
|
---|
12 |
|
---|
13 | public delegate void LockExitEventHandler();
|
---|
14 |
|
---|
15 | /// <summary>
|
---|
16 | /// Utility class that encapsulates critical section-like locking
|
---|
17 | /// and an event that gets fired after we exit the lock. Its
|
---|
18 | /// purpose in life is to delay calling event handlers until after
|
---|
19 | /// we exit the lock. If you call event handlers while you hold a
|
---|
20 | /// lock it's easy to deadlock. Those event handlers could
|
---|
21 | /// choose to block on something on a different thread that's
|
---|
22 | /// waiting on our lock.
|
---|
23 | /// </summary>
|
---|
24 | public sealed class CallbackLock : IDisposable
|
---|
25 | {
|
---|
26 | private readonly object lockObject;
|
---|
27 |
|
---|
28 | public CallbackLock(object lockTarget)
|
---|
29 | {
|
---|
30 | this.lockObject = lockTarget;
|
---|
31 | Monitor.Enter(lockTarget);
|
---|
32 | }
|
---|
33 |
|
---|
34 | [SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly", Justification = "Helper event to defer actions until after lock exit doesn't need arguments")]
|
---|
35 | public event LockExitEventHandler LockExit;
|
---|
36 |
|
---|
37 | public void Dispose()
|
---|
38 | {
|
---|
39 | Monitor.Exit(lockObject);
|
---|
40 | if (LockExit != null)
|
---|
41 | {
|
---|
42 | LockExit();
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.