Click here to Skip to main content
15,881,769 members
Home / Discussions / C#
   

C#

 
GeneralFill the List with Data Set Pin
Birdy3-Sep-03 20:25
Birdy3-Sep-03 20:25 
GeneralRe: Fill the List with Data Set Pin
Braulio Dez3-Sep-03 23:52
Braulio Dez3-Sep-03 23:52 
GeneralDirectX question Pin
Meysam Mahfouzi3-Sep-03 17:21
Meysam Mahfouzi3-Sep-03 17:21 
GeneralRe: DirectX question Pin
TimK3-Sep-03 18:00
TimK3-Sep-03 18:00 
GeneralRe: DirectX question Pin
Meysam Mahfouzi3-Sep-03 18:23
Meysam Mahfouzi3-Sep-03 18:23 
GeneralHelp!!!!!!(at C# of environment use VB function ) Pin
simonasp3-Sep-03 17:12
simonasp3-Sep-03 17:12 
GeneralRe: Help!!!!!!(at C# of environment use VB function ) Pin
Corinna John4-Sep-03 1:48
Corinna John4-Sep-03 1:48 
GeneralThread safety Pin
TimK3-Sep-03 14:34
TimK3-Sep-03 14:34 
Hi, I am trying to get my head around safe thread synchronization.
I think the code below is not thread safe as the Start() method may be called several times which would cause the event to be fired asynchronously.

Any comments?
public class ThreadTest1
{
  public void Start()
  {
    Thread thread = new Thread( new ThreadStart(ThreadMethod));
    thread.Start();
  }

  private void ThreadMethod()
  {
    while (true) 
    {
      Thread.Sleep(1000);
      OnEvent(EventArgs.Empty); 
    }
  }

  public event EventHandler Event;
  protected virtual void OnEvent(EventArgs e)
  {
    if (Event != null)	
    {
      Event(this, e);
    }
  }
}

Would the code below be safe?
I have wrapped the event code in a locked block.
public class ThreadTest2
{
  public void Start()
  {
    Thread thread = new Thread( new ThreadStart(ThreadMethod));
    thread.Start();
  }

  private void ThreadMethod()
  {
    while (true) 
    {
      Thread.Sleep(1000);
      DoEvent(); 
    }
  }

  private void DoEvent()
  {
    lock (this)
    {
      OnEvent(EventArgs.Empty); 
    }
  }

  public event EventHandler Event;
  protected virtual void OnEvent(EventArgs e)
  {
    if (Event != null)	
    {
      Event(this, e);
    }
  }
}

Any comments would be appreciated
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 
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 

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.