|
/////////////////////////////////////////////////////////////////////////////
//
// (c) 2007 BinaryComponents Ltd. All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
namespace BinaryComponents.Utility.Control
{
public sealed class Flag
{
public bool IsActive
{
get
{
lock( _lock )
{
return _count > 0;
}
}
}
public IDisposable Apply()
{
int count;
lock( _lock )
{
++_count;
count = _count;
}
if( count == 1 && Set != null )
{
Set( this, EventArgs.Empty );
}
return new Disposer( this );
}
private sealed class Disposer : IDisposable
{
internal Disposer( Flag flag )
{
_flag = flag;
}
#region IDisposable Members
public void Dispose()
{
if( _flag != null )
{
int count;
lock( _flag._lock )
{
--_flag._count;
count = _flag._count;
}
if( count == 0 && _flag.Reset != null )
{
_flag.Reset( _flag, EventArgs.Empty );
}
_flag = null;
}
}
#endregion
private Flag _flag;
}
public event EventHandler Set;
public event EventHandler Reset;
private int _count;
private object _lock = new object();
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm currently working for a small start-up company, BinaryComponents Ltd, producing the FeedGhost RSS reader.
FeedGhost RSS Reader:
http://www.feedghost.com
Bespoke Software Development
http://www.binarycomponents.com