[Modify] Polish it
This commit is contained in:
parent
6ff198c482
commit
e9bea026aa
@ -11,16 +11,16 @@ namespace Example1
|
||||
internal class Notifier : IDisposable
|
||||
{
|
||||
private volatile bool _enabled;
|
||||
private ManualResetEvent _exited;
|
||||
private Queue<NotificationMessage> _queue;
|
||||
private object _sync;
|
||||
private ManualResetEvent _waitHandle;
|
||||
|
||||
public Notifier ()
|
||||
{
|
||||
_enabled = true;
|
||||
_exited = new ManualResetEvent (false);
|
||||
_queue = new Queue<NotificationMessage> ();
|
||||
_sync = ((ICollection) _queue).SyncRoot;
|
||||
_waitHandle = new ManualResetEvent (false);
|
||||
|
||||
ThreadPool.QueueUserWorkItem (
|
||||
state => {
|
||||
@ -40,8 +40,9 @@ namespace Example1
|
||||
}
|
||||
}
|
||||
|
||||
_waitHandle.Set ();
|
||||
});
|
||||
_exited.Set ();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public int Count {
|
||||
@ -60,16 +61,17 @@ namespace Example1
|
||||
public void Close ()
|
||||
{
|
||||
_enabled = false;
|
||||
_waitHandle.WaitOne ();
|
||||
_waitHandle.Close ();
|
||||
_exited.WaitOne ();
|
||||
_exited.Close ();
|
||||
}
|
||||
|
||||
public void Notify (NotificationMessage message)
|
||||
{
|
||||
lock (_sync)
|
||||
lock (_sync) {
|
||||
if (_enabled)
|
||||
_queue.Enqueue (message);
|
||||
}
|
||||
}
|
||||
|
||||
void IDisposable.Dispose ()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user