none
Variable as thread-safe RRS feed

  • Question

  • Hello,

    What means thread-safe in below code?

    // Copy to a temporary variable to be thread-safe.
            EventHandler<MyEventArgs> temp = SampleEvent;

    using System;
    using System.Collections.Generic;
    
    //---------------------------------------------------------
    public class MyEventArgs : EventArgs
    {
        private string msg;
    
        public MyEventArgs( string messageData ) {
            msg = messageData;
        }
        public string Message { 
            get { return msg; } 
            set { msg = value; }
        }
    }
    //---------------------------------------------------------
    public class HasEvent
    {
    // Declare an event of delegate type EventHandler of 
    // MyEventArgs.
    
        public event EventHandler<MyEventArgs> SampleEvent;
    
        public void DemoEvent(string val)
        {
        // Copy to a temporary variable to be thread-safe.
            EventHandler<MyEventArgs> temp = SampleEvent;
            if (temp != null)
                temp(this, new MyEventArgs(val));
        }
    }
    //---------------------------------------------------------
    public class Sample
    {
        public static void Main()
        {
            HasEvent he = new HasEvent();
            he.SampleEvent += 
                       new EventHandler<MyEventArgs>(SampleEventHandler);
            he.DemoEvent("Hey there, Bruce!");
            he.DemoEvent("How are you today?");
            he.DemoEvent("I'm pretty good.");
            he.DemoEvent("Thanks for asking!");
        }
        private static void SampleEventHandler(object src, MyEventArgs mea)
        {
            Console.WriteLine(mea.Message);
        }
    }

    Sunday, March 25, 2018 8:05 PM

Answers

  • You probably are interested in the difference between your ‘if’ and this one:

       if( SampleEvent != null)
          SampleEvent( this, new MyEventArgs(val));

    This is not safe in the next scenario: if the first line is executed, but then another parallel thread sets SampleEvent to null, then the second line will fail because of null SampleEvent.

    An alternative modern solution is a single line:

       SampleEvent?.Invoke( this, new MyEventArgs(val));


    • Edited by Viorel_MVP Monday, March 26, 2018 5:11 AM
    • Marked as answer by Arash_89 Monday, March 26, 2018 12:37 PM
    Monday, March 26, 2018 5:10 AM

All replies

  • Hello,

     Take the time to read the article at the following URL;

     

     https://msdn.microsoft.com/en-us/library/f857xew0(v=vs.71).aspx

     

     After reading, you should have a better understanding of Thread-Safe.

    There are also code examples in VB and C# so please read carefully.

     

     Hope this helps :)

    Monday, March 26, 2018 2:54 AM
  • You probably are interested in the difference between your ‘if’ and this one:

       if( SampleEvent != null)
          SampleEvent( this, new MyEventArgs(val));

    This is not safe in the next scenario: if the first line is executed, but then another parallel thread sets SampleEvent to null, then the second line will fail because of null SampleEvent.

    An alternative modern solution is a single line:

       SampleEvent?.Invoke( this, new MyEventArgs(val));


    • Edited by Viorel_MVP Monday, March 26, 2018 5:11 AM
    • Marked as answer by Arash_89 Monday, March 26, 2018 12:37 PM
    Monday, March 26, 2018 5:10 AM