[Modify] Polish it

This commit is contained in:
sta 2016-08-03 15:32:17 +09:00
parent 6ff198c482
commit e9bea026aa

View File

@ -11,16 +11,16 @@ namespace Example1
internal class Notifier : IDisposable internal class Notifier : IDisposable
{ {
private volatile bool _enabled; private volatile bool _enabled;
private ManualResetEvent _exited;
private Queue<NotificationMessage> _queue; private Queue<NotificationMessage> _queue;
private object _sync; private object _sync;
private ManualResetEvent _waitHandle;
public Notifier () public Notifier ()
{ {
_enabled = true; _enabled = true;
_exited = new ManualResetEvent (false);
_queue = new Queue<NotificationMessage> (); _queue = new Queue<NotificationMessage> ();
_sync = ((ICollection) _queue).SyncRoot; _sync = ((ICollection) _queue).SyncRoot;
_waitHandle = new ManualResetEvent (false);
ThreadPool.QueueUserWorkItem ( ThreadPool.QueueUserWorkItem (
state => { state => {
@ -40,8 +40,9 @@ namespace Example1
} }
} }
_waitHandle.Set (); _exited.Set ();
}); }
);
} }
public int Count { public int Count {
@ -60,15 +61,16 @@ namespace Example1
public void Close () public void Close ()
{ {
_enabled = false; _enabled = false;
_waitHandle.WaitOne (); _exited.WaitOne ();
_waitHandle.Close (); _exited.Close ();
} }
public void Notify (NotificationMessage message) public void Notify (NotificationMessage message)
{ {
lock (_sync) lock (_sync) {
if (_enabled) if (_enabled)
_queue.Enqueue (message); _queue.Enqueue (message);
}
} }
void IDisposable.Dispose () void IDisposable.Dispose ()