Guest User

Untitled

a guest
Jan 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class ClassB
  2. {
  3. private ClassA A { get; set; }
  4.  
  5. public ClassB()
  6. {
  7. this.A = new ClassA();
  8. this.A.OnProcessed += new ClassA.DelegateProcessed(this.ClassA_Processed);
  9. }
  10.  
  11. public void Process()
  12. {
  13. this.A.Process();
  14. }
  15.  
  16. public void ClassA_Processed (ClassA sender, EventArgs e)
  17. {
  18. // Do something.
  19.  
  20. // Code written by another developer does not free up events before calling Dispose.
  21.  
  22. this.A.Dispose();
  23. this.A = null;
  24. }
  25. }
  26.  
  27. public class ClassA: IDisposable
  28. {
  29. public delegate void DelegateProcessed (A sender, EventArgs e);
  30. public event DelegateProcessed OnProcessed = null;
  31.  
  32. ~ClassA() { this.Dispose(false); }
  33.  
  34. public void Dispose ()
  35. {
  36. this.Dispose(true);
  37. System.GC.SuppressFinalize(this);
  38. }
  39.  
  40. private void Dispose (bool disposing)
  41. {
  42. if (!this.Disposed)
  43. {
  44. if (disposing)
  45. {
  46. // Dispose managed resources here.
  47. // Is it possible / advisable to dispose of delegates / events here?
  48. // Will this adversely affect the consumer class?
  49. this.OnProcessed -= new ClassA.DelegateProcessed(this.ClassA_Processed);
  50. }
  51. }
  52. this.Disposed = true;
  53. }
  54.  
  55. public void Process () { this.OnProcessed(this, new EventArgs()); }
  56.  
  57. public void ClassA_Processed (ClassA sender, EventArgs e) { }
  58. }
  59.  
  60. private ClassA A { get; set; }
  61.  
  62. public ClassB()
  63. {
  64. this.A = new ClassA();
  65. this.A.OnProcessed += new ClassA.DelegateProcessed(this.ClassA_Processed);
  66. }
  67.  
  68. // class B
  69. this.A.OnProcessed += new ClassA.DelegateProcessed(this.ClassA_Processed);
  70.  
  71. // in classA
  72. this.OnProcessed -= new ClassA.DelegateProcessed(this.ClassA_Processed);
Add Comment
Please, Sign In to add comment