ZmqPoller.cs 814 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using NetMQ;
  3. namespace Zmq
  4. {
  5. public class ZmqPoller: Poller
  6. {
  7. private readonly object eventsLock = new object();
  8. private Action events;
  9. public ZmqPoller()
  10. {
  11. AddTimer();
  12. }
  13. private void AddTimer()
  14. {
  15. var timer = new NetMQTimer(TimeSpan.FromMilliseconds(10));
  16. timer.Elapsed += (sender, args) => this.OnEvents();
  17. AddTimer(timer);
  18. }
  19. public event Action Events
  20. {
  21. add
  22. {
  23. lock (this.eventsLock)
  24. {
  25. this.events += value;
  26. }
  27. }
  28. remove
  29. {
  30. lock (this.eventsLock)
  31. {
  32. this.events -= value;
  33. }
  34. }
  35. }
  36. private void OnEvents()
  37. {
  38. Action local = null;
  39. lock (this.eventsLock)
  40. {
  41. if (this.events == null)
  42. {
  43. return;
  44. }
  45. local = this.events;
  46. this.events = null;
  47. }
  48. local();
  49. AddTimer();
  50. }
  51. }
  52. }