イベントをトリガーし、イベントを登録したかどうかを確認する方法

7935 ワード

次のコードでは、イベントが登録されていないことがわかります.
 class Program

    {

        static void Main(string[] args)

        {

            EventCheck check = new EventCheck();

            check.OnDataArrived(new DataArrivedEventArgs() { data="Hello World!"});

            Console.ReadKey();

        }



        static void check_DataArrived(object sender, DataArrivedEventArgs e)

        {

            Console.WriteLine(e.data);

        }

    }



    class EventCheck

    {

        //  use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

        //To associate the event with the method that will handle the event, add an instance of the delegate to the event. 

        //The event handler is called whenever the event occurs, unless you remove the delegate.

        internal event DataArrivedEventHandler DataArrived;

        

        // 

        internal void OnDataArrived(DataArrivedEventArgs e)

        {

            try

            {

                //DataArrivedEventHandler handler = DataArrived;

                //if (handler != null)

                //{

                //    handler(this, e);

                //}

                DataArrived += EventCheck_DataArrived;

                if (DataArrived != null)

                {

                    DataArrived -= EventCheck_DataArrived;

                    DataArrived(this, e);

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(" {0}", ex.Message);

            }

        }



        void EventCheck_DataArrived(object sender, DataArrivedEventArgs e)

        {

            Console.WriteLine(e.data);

        }

    }



    //    Represents the method that will handle an event when the event provides data.

    //This method's first parameter is of type Object and refers to the instance that raises the event. 

    //Its second parameter is derived from type EventArgs and holds the event data.

    //If the event does not generate event data, the second parameter is simply the value of the EventArgs.Empty field. 

    //Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.

    delegate void DataArrivedEventHandler(object sender,DataArrivedEventArgs e);  // , EventHandler<TEventArgs> 



    //    

    //create a custom event data class, create a class that derives from the EventArgs class and provide the properties to store the necessary data. 

    //The name of your custom event data class should end with EventArgs.

    class DataArrivedEventArgs : EventArgs

    {

       internal string data;

    }

 
 
ステップ4のコードを修正し、イベントを一時変数にコピーしてから、この一時変数で処理すれば問題ありません.
// 

        internal void OnDataArrived(DataArrivedEventArgs e)

        {

            try

            {

                //DataArrivedEventHandler handler = DataArrived;

                //if (handler != null)

                //{

                //    handler(this, e);

                //}

                DataArrived += EventCheck_DataArrived;

                DataArrivedEventHandler handler = DataArrived;

                if (handler != null)

                {

                    DataArrived -= EventCheck_DataArrived;

                    handler(this, e);

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(" {0}", ex.Message);

            }

        }