Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. public class Foo
  2. {
  3. public event EventHandler SomeEvent;
  4. public Foo()
  5. {
  6. SomeEvent += FooHandlers.Foo_SomeEvent1;
  7. SomeEvent += FooHandlers.Foo_SomeEvent2;
  8. }
  9.  
  10. public void RaiseEvents(EventHandler evt, EventArgs args)
  11. {
  12. var eventObj = evt;
  13. var listeners = eventObj.GetInvocationList();
  14. foreach (var listener in listeners)
  15. {
  16. var method = (EventHandler)listener;
  17. ThreadPool.QueueUserWorkItem(callBack => method(this, args));
  18. // Handlers will do a lot of things, so I don't want
  19. // them blocking the Main thread
  20. }
  21. }
  22.  
  23. public void SomeMethod()
  24. {
  25. // do something here
  26. RaiseEvents(SomeEvent, new EventArgs());
  27. }
  28. }
  29.  
  30. public static class FooHandlers
  31. {
  32. public static void Foo_SomeEvent1(object sender, EventArgs e)
  33. {
  34. //do something here
  35. }
  36. public static void Foo_SomeEvent2(object sender, EventArgs e)
  37. {
  38. //do something different here
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement