-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathNotifier.cs
More file actions
79 lines (70 loc) · 1.64 KB
/
Notifier.cs
File metadata and controls
79 lines (70 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#if UBUNTU
using Notifications;
#endif
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace Example
{
internal class Notifier : IDisposable
{
private volatile bool _enabled;
private Queue<NotificationMessage> _queue;
private object _sync;
private ManualResetEvent _waitHandle;
public Notifier ()
{
_enabled = true;
_queue = new Queue<NotificationMessage> ();
_sync = ((ICollection) _queue).SyncRoot;
_waitHandle = new ManualResetEvent (false);
ThreadPool.QueueUserWorkItem (
state => {
while (_enabled || Count > 0) {
var msg = dequeue ();
if (msg != null) {
#if UBUNTU
var nf = new Notification (msg.Summary, msg.Body, msg.Icon);
nf.AddHint ("append", "allowed");
nf.Show ();
#else
Console.WriteLine (msg);
#endif
}
else {
Thread.Sleep (500);
}
}
_waitHandle.Set ();
});
}
public int Count {
get {
lock (_sync)
return _queue.Count;
}
}
private NotificationMessage dequeue ()
{
lock (_sync)
return _queue.Count > 0 ? _queue.Dequeue () : null;
}
public void Close ()
{
_enabled = false;
_waitHandle.WaitOne ();
_waitHandle.Close ();
}
public void Notify (NotificationMessage message)
{
lock (_sync)
if (_enabled)
_queue.Enqueue (message);
}
void IDisposable.Dispose ()
{
Close ();
}
}
}