CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BusyLock.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BusyLock.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Implementation of a lock that waits while</summary>
7//-----------------------------------------------------------------------
8using System;
9using Csla.Core;
10using System.Threading;
11
12namespace Csla.Threading
13{
22 public static class BusyLock
23 {
29 public static void WaitOne(INotifyBusy obj)
30 {
31 BusyLocker locker = new BusyLocker(obj, TimeSpan.FromMilliseconds(Timeout.Infinite));
32 locker.WaitOne();
33 }
34
41 public static void WaitOne(INotifyBusy obj, TimeSpan timeout)
42 {
43 BusyLocker locker = new BusyLocker(obj, timeout);
44 locker.WaitOne();
45 }
46 }
47
52 public class BusyLocker : IDisposable
53 {
54 private ManualResetEvent _event = new ManualResetEvent(false);
55 private INotifyBusy _target;
56 private TimeSpan _timeout;
57
63 public BusyLocker(INotifyBusy target, TimeSpan timeout)
64 {
65 _event.Reset(); // set the event to non-signaled by default.
66 _target = target;
67 _timeout = timeout;
68 }
69
73 public void WaitOne()
74 {
75 try
76 {
77 _target.BusyChanged += new BusyChangedEventHandler(notify_BusyChanged);
78
79 // Do nothing if this object is not currently busy
80 // otherwise wait for the event to be signaled.
81 if (_target.IsBusy)
82 {
83#if (ANDROID || IOS)
84 _event.WaitOne(_timeout);
85#else
86 _event.WaitOne(_timeout, false);
87#endif
88 }
89 }
90 finally
91 {
92 _target.BusyChanged -= new BusyChangedEventHandler(notify_BusyChanged);
93 _event.Close();
94 }
95 }
96
97 private void notify_BusyChanged(object sender, BusyChangedEventArgs e)
98 {
99 // If the object is not busy then trigger
100 // the event to unblock the calling thread.
101 if (!_target.IsBusy)
102 _event.Set();
103 }
104
108 public void Dispose()
109 {
110 WaitOne();
111 }
112 }
113}
Event arguments for the BusyChanged event.
Implementation of a lock that waits while a target object is busy.
Definition: BusyLock.cs:53
void WaitOne()
Waits for the target object to become not busy.
Definition: BusyLock.cs:73
BusyLocker(INotifyBusy target, TimeSpan timeout)
Creates an instance of the object.
Definition: BusyLock.cs:63
void Dispose()
Disposes the object.
Definition: BusyLock.cs:108
Interface defining an object that notifies when it is busy executing an asynchronous operation.
Definition: INotifyBusy.cs:17
BusyChangedEventHandler BusyChanged
Event raised when the object's busy status changes.
Definition: INotifyBusy.cs:22
bool IsBusy
Gets a value indicating whether the object, or any of the object's child objects, are busy running an...
Definition: INotifyBusy.cs:29
delegate void BusyChangedEventHandler(object sender, BusyChangedEventArgs e)
Delegate for handling the BusyChanged event.