Designing a type which exposes an event p.2

Example 4:

 

    public class EventsType

    {

        public  event EventHandler<MyEventArgs<Object>> NewEvent;

 

        public void SmthHappened(string data)

        {

            MyEventArgs<Object> eventArgs = new MyEventArgs<Object>(data);           

            OnNewEvent(eventArgs);

        }

 

        protected virtual void OnNewEvent(MyEventArgs<Object> eventArgs)

        {

            EventHandler<MyEventArgs<Object>> tempEvent = NewEvent;

            Thread.Sleep(0);

            if (NewEvent == null && tempEvent != null)

            {

                Trace.WriteLine("It happened!");

                tempEvent(this, eventArgs); 

            }                                                                 

        }

    }

 

 

    class Worker

    {

        public static void DoRegisterWork(Object t)

        {

            EventHandlerType eventHandler = (EventHandlerType)t;

            int i = 0;

            while (i < 5000)

            {

                Trace.WriteLine("Call register: " + i);  

                eventHandler.RegisterEvent();

                eventHandler.EventsType.SmthHappened(i + ". Register");               

                i++;

                Thread.Sleep(10);

            }                          

        }

 

        public static void DoUnregisterWork(Object t)

        {

            EventHandlerType eventHandler = (EventHandlerType)t;           

            int i = 0;

            while (i < 5000)

            {

                Trace.WriteLine("Call unregister: " + i);  

                eventHandler.UnregisterEvent();

                eventHandler.EventsType.SmthHappened(i + ". Unregister");

                i++;

                Thread.Sleep(0);

            }

        }

    }

 

    class EventHandlerType

    {

 

        private EventsType _eventsType = null;

 

        public EventHandlerType(EventsType eventsType)

        {

            _eventsType = eventsType;

        }

 

        public EventsType EventsType

        {

            get { return _eventsType; }

        }

 

        public void RegisterEvent()

        {

            _eventsType.NewEvent += CallbackMethod;   

        }

 

        public void UnregisterEvent()

        {

            _eventsType.NewEvent -= CallbackMethod;

        }

               

        private void CallbackMethod(Object sender, MyEventArgs<Object> args)

        {           

            Trace.WriteLine(args.Data);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

 

            EventsType eventsType = new EventsType();

            EventHandlerType eventHandlerType = new EventHandlerType(eventsType);

 

            Thread thread1 = new Thread(new ParameterizedThreadStart(Worker.DoRegisterWork));

            thread1.IsBackground = true;

            thread1.Name = "1";

 

            Thread thread2 = new Thread(new ParameterizedThreadStart(Worker.DoUnregisterWork));

            thread2.Name = "2";

            thread2.IsBackground = true;

 

            thread2.Start(eventHandlerType);

            thread1.Start(eventHandlerType);

 

            Thread.Sleep(0);

 

            Console.ReadKey();

        }

    }

 

Call unregister: 0

Call register: 0

It happened!

0. Register

Call unregister: 1

Call unregister: 2

Call unregister: 3

Call unregister: 4

Call unregister: 5

Call unregister: 6

Call unregister: 7

Call unregister: 8

Call unregister: 9

Call unregister: 10

Call unregister: 11

Call unregister: 12

Call register: 1

Call unregister: 13

Call unregister: 14

Call unregister: 15

Call unregister: 16

Call unregister: 17

Call unregister: 18

Call unregister: 19

Call unregister: 20

Call unregister: 21

Call unregister: 22

It happened!

1.       Register

 

 

            Define a method which will send data for the event.

 

Example 5:

 

        public void SmthHappened(string data)

        {

            MyEventArgs<Object> eventArgs = new MyEventArgs<Object>(data);           

            OnNewEvent(eventArgs);

        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s