Click here to Skip to main content
16,005,339 members
Home / Discussions / C#
   

C#

 
GeneralRe: And also consider this: Pin
TimK4-Sep-03 18:11
TimK4-Sep-03 18:11 
GeneralRe: And also consider this: Pin
Meysam Mahfouzi4-Sep-03 18:22
Meysam Mahfouzi4-Sep-03 18:22 
GeneralRe: And also consider this: Pin
TimK4-Sep-03 18:30
TimK4-Sep-03 18:30 
GeneralRe: And also consider this: Pin
Meysam Mahfouzi5-Sep-03 17:15
Meysam Mahfouzi5-Sep-03 17:15 
GeneralRe: And also consider this: Pin
scadaguy5-Sep-03 4:25
scadaguy5-Sep-03 4:25 
GeneralRe: And also consider this: Pin
Meysam Mahfouzi5-Sep-03 17:25
Meysam Mahfouzi5-Sep-03 17:25 
GeneralRe: And also consider this: Pin
scadaguy5-Sep-03 4:28
scadaguy5-Sep-03 4:28 
GeneralRe: And also consider this: Pin
scadaguy5-Sep-03 4:22
scadaguy5-Sep-03 4:22 
I mentioned this problem briefly in my original post. My example code assumes that all event handlers have been added prior to calling ThreadTest.Start(). It's easy is see that unsubscribing with -= will cause a lot of problems after the threads have been started, but += could be problematic as well. This problem is difficult solve correctly and that is why I suggested to intentionally leave the ThreadTest class unsafe for multithreading.

Simply locking the code that raises the event is insufficient. You must lock the -= and += operators as well. This can be done with special syntax, using the add and remove keywords.

public class ThreadTest
{
  private ArrayList _events = new ArrayList();

  public event EventHandler MyEvent
  {
    add { lock (this) { _events.Add(value); } }
    remove { lock (this) { _events.Remove(value); } }
  }

  public void Start()
  {
    // Same code as before
  }

  private void ThreadMethod()
  {
    while (true)
    {
      ArrayList clone;
      lock (this)
      {
        clone = (ArrayList)_events.Clone();
      }
      foreach (EventHandler e in clone)
      {
        e.DynamicInvoke(...);
      }
    }
  }
}


If you're going to do this you might as well make every method on ThreadTest thread-safe to avoid confusion and inconsistency. This solution still requires that subscribers handle their on synchronization.

Brian
GeneralDataGrid alterations... Pin
james-cxx3-Sep-03 13:11
james-cxx3-Sep-03 13:11 
GeneralRe: DataGrid alterations... Pin
A.Wegierski3-Sep-03 19:21
A.Wegierski3-Sep-03 19:21 
GeneralRe: DataGrid alterations... Pin
Braulio Dez3-Sep-03 23:58
Braulio Dez3-Sep-03 23:58 
GeneralRe: DataGrid alterations... Pin
james-cxx4-Sep-03 7:10
james-cxx4-Sep-03 7:10 
GeneralListViews: Sort ascending/descending Pin
tsigo3-Sep-03 10:36
tsigo3-Sep-03 10:36 
GeneralRe: ListViews: Sort ascending/descending Pin
Furty3-Sep-03 20:08
Furty3-Sep-03 20:08 
GeneralRe: ListViews: Sort ascending/descending Pin
tsigo6-Sep-03 11:00
tsigo6-Sep-03 11:00 
GeneralObtaining Caller of a Method in C# Pin
TheLastBastion3-Sep-03 9:21
TheLastBastion3-Sep-03 9:21 
GeneralRe: Obtaining Caller of a Method in C# Pin
leppie3-Sep-03 10:16
leppie3-Sep-03 10:16 
GeneralRe: Obtaining Caller of a Method in C# Pin
James Simpson3-Sep-03 10:28
James Simpson3-Sep-03 10:28 
GeneralRe: Obtaining Caller of a Method in C# Pin
leppie3-Sep-03 13:33
leppie3-Sep-03 13:33 
GeneralRe: Obtaining Caller of a Method in C# Pin
James T. Johnson3-Sep-03 14:32
James T. Johnson3-Sep-03 14:32 
GeneralRe: Obtaining Caller of a Method in C# Pin
Jerry Dennany3-Sep-03 14:34
Jerry Dennany3-Sep-03 14:34 
GeneralPInvoke Pin
Anonymous3-Sep-03 9:17
Anonymous3-Sep-03 9:17 
GeneralDesign Patterns - on a mission. Pin
Aroogala3-Sep-03 6:58
Aroogala3-Sep-03 6:58 
GeneralRe: Design Patterns - on a mission. Pin
Anonymous3-Sep-03 7:37
Anonymous3-Sep-03 7:37 
GeneralRe: Design Patterns - on a mission. Pin
Jim Stewart4-Sep-03 3:27
Jim Stewart4-Sep-03 3:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.