Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. why do the custom written events NEED to have a client??

why do the custom written events NEED to have a client??

Scheduled Pinned Locked Moved C#
csharpquestion
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    michal kreslik
    wrote on last edited by
    #1

    Hello, I have just found the custom-written event in C# needs to have at least one client registered to consume the event or else the application will crash. Why this is so? This means that if I unregister all the client methods from the event during the time the application is running and then the event occurs, it will crash. Or am I missing something? Evidence that this is so - test bench program:

    using System;

    namespace EventsTest
    {
    public delegate void FirstEventHandler();
    public delegate void SecondEventHandler();

    public class EventsClass 
    { 
        public event FirstEventHandler FirstEvent; 
        public event SecondEventHandler SecondEvent; 
    
        private void FireOffFirstEvent() 
        { 
            FirstEvent(); 
        } 
    
        private void FireOffSecondEvent() 
        { 
            SecondEvent(); 
        } 
    
        public void FireOffAllEvents() 
        { 
            FireOffFirstEvent(); 
            FireOffSecondEvent(); 
        } 
    } 
    
    public class EntryClass 
    { 
        static void Main() 
        { 
            EventsClass MyEvents = new EventsClass(); 
            string MyChoice; 
    
            do 
            { 
                Console.Write("Do you wish to register the event clients to the events? (y/n): "); 
                MyChoice = Console.ReadLine(); 
    
            } while (MyChoice != "y" && MyChoice != "n"); 
    
            switch (MyChoice) 
            { 
                case "y": 
    
                    MyEvents.FirstEvent += new FirstEventHandler(FirstEventClient); 
                    Console.WriteLine("First event registered."); 
    
                    MyEvents.SecondEvent += new SecondEventHandler(SecondEventClient); 
                    Console.WriteLine("Second event registered."); 
    
                    break; 
    
                case "n": 
    
                    Console.WriteLine("No events registered."); 
    
                    break; 
            } 
    
            Console.WriteLine("Firing off all events.."); 
    
            MyEvents.FireOffAllEvents(); 
    
            Console.WriteLine("Press enter to continue.."); 
            Console.ReadLine(); 
        } 
    
        static void FirstEventClient() 
        { 
            Console.WriteLine("First event processed successfully."); 
        } 
    
        static void SecondEventClient() 
        { 
            Console.WriteLine("Second event processed successfully."); 
        } 
    } 
    

    }

    Q 1 Reply Last reply
    0
    • M michal kreslik

      Hello, I have just found the custom-written event in C# needs to have at least one client registered to consume the event or else the application will crash. Why this is so? This means that if I unregister all the client methods from the event during the time the application is running and then the event occurs, it will crash. Or am I missing something? Evidence that this is so - test bench program:

      using System;

      namespace EventsTest
      {
      public delegate void FirstEventHandler();
      public delegate void SecondEventHandler();

      public class EventsClass 
      { 
          public event FirstEventHandler FirstEvent; 
          public event SecondEventHandler SecondEvent; 
      
          private void FireOffFirstEvent() 
          { 
              FirstEvent(); 
          } 
      
          private void FireOffSecondEvent() 
          { 
              SecondEvent(); 
          } 
      
          public void FireOffAllEvents() 
          { 
              FireOffFirstEvent(); 
              FireOffSecondEvent(); 
          } 
      } 
      
      public class EntryClass 
      { 
          static void Main() 
          { 
              EventsClass MyEvents = new EventsClass(); 
              string MyChoice; 
      
              do 
              { 
                  Console.Write("Do you wish to register the event clients to the events? (y/n): "); 
                  MyChoice = Console.ReadLine(); 
      
              } while (MyChoice != "y" && MyChoice != "n"); 
      
              switch (MyChoice) 
              { 
                  case "y": 
      
                      MyEvents.FirstEvent += new FirstEventHandler(FirstEventClient); 
                      Console.WriteLine("First event registered."); 
      
                      MyEvents.SecondEvent += new SecondEventHandler(SecondEventClient); 
                      Console.WriteLine("Second event registered."); 
      
                      break; 
      
                  case "n": 
      
                      Console.WriteLine("No events registered."); 
      
                      break; 
              } 
      
              Console.WriteLine("Firing off all events.."); 
      
              MyEvents.FireOffAllEvents(); 
      
              Console.WriteLine("Press enter to continue.."); 
              Console.ReadLine(); 
          } 
      
          static void FirstEventClient() 
          { 
              Console.WriteLine("First event processed successfully."); 
          } 
      
          static void SecondEventClient() 
          { 
              Console.WriteLine("Second event processed successfully."); 
          } 
      } 
      

      }

      Q Offline
      Q Offline
      Qhalis
      wrote on last edited by
      #2

      From the code, it looks like you should be getting an unhandled null reference exception in FireOffFirstEvent() when you do not register the events. This is correct. When you create the instance of EventsClass, FirstEvent is null. If you do not register the events it is never set. You call FireOffAllEvents() which calls FireOffFirstEvent() which calls FirstEvent() - which is null One way to solve this is place a check around the delegate invocation, e.g. private void FireOffFirstEvent() { if (null != FirstEvent) { FirstEvent(); } } Q.

      M 1 Reply Last reply
      0
      • Q Qhalis

        From the code, it looks like you should be getting an unhandled null reference exception in FireOffFirstEvent() when you do not register the events. This is correct. When you create the instance of EventsClass, FirstEvent is null. If you do not register the events it is never set. You call FireOffAllEvents() which calls FireOffFirstEvent() which calls FirstEvent() - which is null One way to solve this is place a check around the delegate invocation, e.g. private void FireOffFirstEvent() { if (null != FirstEvent) { FirstEvent(); } } Q.

        M Offline
        M Offline
        michal kreslik
        wrote on last edited by
        #3

        OK. But then why there's not the same problem with not registering all the built-in events that come with, say, a windows form? Does that mean that VisualStudio doesn't even declare these events until I use them and register them? Michal

        M 1 Reply Last reply
        0
        • M michal kreslik

          OK. But then why there's not the same problem with not registering all the built-in events that come with, say, a windows form? Does that mean that VisualStudio doesn't even declare these events until I use them and register them? Michal

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          It's exactly the same with the "built-in" events. Every example for events I know and every place in the BCL where events are invoked first checks if there's an event handler attached. It has something to do with how the list of event listeners is managed.

          Regards, mav -- Black holes are the places where god divided by 0...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups