Click here to Skip to main content
15,891,253 members
Home / Discussions / C#
   

C#

 
GeneralThread safety Pin
TimK3-Sep-03 14:34
TimK3-Sep-03 14:34 
GeneralRe: Thread safety Pin
Meysam Mahfouzi3-Sep-03 17:11
Meysam Mahfouzi3-Sep-03 17:11 
GeneralRe: Thread safety Pin
TimK3-Sep-03 17:56
TimK3-Sep-03 17:56 
GeneralRe: Thread safety Pin
Meysam Mahfouzi3-Sep-03 18:35
Meysam Mahfouzi3-Sep-03 18:35 
GeneralRe: Thread safety Pin
TimK3-Sep-03 19:00
TimK3-Sep-03 19:00 
GeneralRe: Thread safety Pin
scadaguy4-Sep-03 6:57
scadaguy4-Sep-03 6:57 
GeneralRe: Thread safety Pin
TimK4-Sep-03 13:30
TimK4-Sep-03 13:30 
GeneralRe: Thread safety Pin
scadaguy4-Sep-03 7:28
scadaguy4-Sep-03 7:28 
I would intentionally leave the ThreadTest class unsafe for multithreading. Inform the subscribers of this class' event that it is executed on another thread. Let the subscribers handle their own synchronization. Remember, the ThreadTest class is not safe for multithreading so that means subscribing and unsubscribing to the event is also not safe multithreading. Here's how the code might look...

public class ThreadTest
{
  public event EventHandler MyEvent;

  public void Start()
  {
    Thread thread = new Thread(new ThreadStart(this.ThreadMethod));
    thread.Start();
  }

  private void ThreadMethod()
  {
    while (true)
    {
      Thread.Sleep(1000);
      if (MyEvent != null) MyEvent(this, EventArgs.Empty);
    }
  }
}

public class Subscriber
{
  public void Run()
  {
    // As a subscriber to ThreadTest.MyEvent I know that whatever
    // delegate I specify for the handler will be executing on an
    // unknown thread. I must take care of my own synchronization.

    ThreadTest test = new ThreadTest();
    test.MyEvent += new EventHandler(this.OnEvent);
    test.Start();
    test.Start();
    test.Start();

    // Continue with the whatever else you need to do here...
  }

  public void OnEvent(object source, EventArgs e)
  {
    // It is NOT safe to access shared members here.

    lock (this)
    {
      // It is safe to access shared members here.
    }
  }
}

GeneralRe: Thread safety Pin
TimK4-Sep-03 13:19
TimK4-Sep-03 13:19 
GeneralAnd also consider this: Pin
Meysam Mahfouzi4-Sep-03 17:44
Meysam Mahfouzi4-Sep-03 17:44 
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 
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 

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.