You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.7 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. #if UBUNTU
  2. using Notifications;
  3. #endif
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. namespace Example1
  9. {
  10. internal class Notifier : IDisposable
  11. {
  12. private volatile bool _enabled;
  13. private Queue<NotificationMessage> _queue;
  14. private object _sync;
  15. private ManualResetEvent _waitHandle;
  16. public Notifier ()
  17. {
  18. _enabled = true;
  19. _queue = new Queue<NotificationMessage> ();
  20. _sync = ((ICollection) _queue).SyncRoot;
  21. _waitHandle = new ManualResetEvent (false);
  22. ThreadPool.QueueUserWorkItem (
  23. state => {
  24. while (_enabled || Count > 0) {
  25. var msg = dequeue ();
  26. if (msg != null) {
  27. #if UBUNTU
  28. var nf = new Notification (msg.Summary, msg.Body, msg.Icon);
  29. nf.AddHint ("append", "allowed");
  30. nf.Show ();
  31. #else
  32. Console.WriteLine (msg);
  33. #endif
  34. }
  35. else {
  36. Thread.Sleep (500);
  37. }
  38. }
  39. _waitHandle.Set ();
  40. });
  41. }
  42. public int Count {
  43. get {
  44. lock (_sync)
  45. return _queue.Count;
  46. }
  47. }
  48. private NotificationMessage dequeue ()
  49. {
  50. lock (_sync)
  51. return _queue.Count > 0
  52. ? _queue.Dequeue ()
  53. : null;
  54. }
  55. public void Close ()
  56. {
  57. _enabled = false;
  58. _waitHandle.WaitOne ();
  59. _waitHandle.Close ();
  60. }
  61. public void Notify (NotificationMessage message)
  62. {
  63. lock (_sync)
  64. if (_enabled)
  65. _queue.Enqueue (message);
  66. }
  67. void IDisposable.Dispose ()
  68. {
  69. Close ();
  70. }
  71. }
  72. }