ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Programming
    CSI-415
    Progress0 / 55 topics
    Topics
    1. Visual Programming Basics2. Introduction to Events3. Fundamentals of Event-Driven Programming4. Message Handling5. User Interfaces6. Graphics Device Interface7. Painting and Drawing8. Windows Management9. Input Devices10. Resources11. String and Menu Resource12. Dialogs and Windows Controls13. Common Controls14. Dynamic Link Libraries (DLLs)15. Threads and Synchronization16. Network Programming17. Building Class Libraries at the Command Line18. Class Libraries19. Using References20. Assemblies21. Private Assembly Deployment22. Shared Assembly Deployment23. Configuration Overview24. Configuration Files25. Programmatic Access to Configuration26. Using SDK Tools for Signing and Deployment27. Metadata28. Reflection29. Late Binding30. Directories and Files31. Serialization32. Attributes33. Memory Management and Garbage Collection34. Threading and Synchronization35. Asynchronous Delegates36. Application Domains37. Marshal by Value38. Marshal by Reference39. Authentication and Authorization40. Configuring Security41. Code Access Security42. Code Groups43. Evidence44. Permissions45. Role-Based Security46. Principals and Identities47. Using Data Readers48. Using Data Sets49. Interacting with XML Data50. Tracing Event Logs51. Using the Boolean Switch and Trace Switch Classes52. Print Debugging Information with the Debug Class53. Instrumenting Release Builds with the Trace Class54. Using Listeners55. Implementing Custom Listeners
    CSI-415›Fundamentals of Event-Driven Programming
    Advanced ProgrammingTopic 3 of 55

    Fundamentals of Event-Driven Programming

    8 minread
    1,300words
    Intermediatelevel

    Fundamentals of Event-Driven Programming

    Event-driven programming (EDP) is a programming paradigm in which the flow of program execution is determined by events. Events are actions or occurrences, such as user interactions, system-generated messages, or messages from other programs, that the program responds to. In an event-driven system, the program's behavior is driven by events, and it waits for these events to happen to trigger actions (or event handlers). This paradigm is particularly common in graphical user interfaces (GUIs) and applications that need to respond to user actions or other asynchronous inputs.

    In C#, event-driven programming is primarily used in GUI applications (Windows Forms, WPF) but can also be applied to asynchronous operations, servers, or other systems that rely on event handling.

    Key Concepts in Event-Driven Programming

    1. Event

    An event is an occurrence within the system, often representing something the program can react to, such as:

    • A mouse click (in a GUI)
    • A keyboard input
    • A file download completion
    • A network request completion

    In event-driven programming, the event is raised (triggered) when it happens, and the system must respond appropriately by executing associated logic.

    2. Event Handlers

    An event handler is a method that is executed when a specific event is triggered. The event handler is a function or procedure that contains the logic you want to run when the event occurs. These are the responses or reactions to the event.

    For example, when a user clicks a button in a GUI, an event handler can open a new window or display a message.

    In C#, event handlers often match the signature of the delegate that is tied to the event.

    Example:

    // Event Handler
    private void ButtonClickHandler(object sender, EventArgs e)
    {
        Console.WriteLine("Button clicked!");
    }
    

    3. Event Source (Publisher)

    The event source is the object that raises or fires the event. It is often the component that detects or triggers the event. For example, in a GUI, a button is an event source because it raises an event (like Click) when a user interacts with it.

    public class Button
    {
        // Declare an event
        public event EventHandler Click;
    
        // Trigger the event
        public void OnClick()
        {
            Click?.Invoke(this, EventArgs.Empty);
        }
    }
    

    4. Event Listener (Subscriber)

    An event listener (also called the subscriber) is an object or method that responds to an event. It subscribes to the event and defines the action to take when the event occurs. In C#, event listeners are typically methods that are attached to events via event subscription.

    // Event listener subscribes to the event
    button.Click += ButtonClickHandler;
    

    5. Delegate

    A delegate is a type-safe function pointer in C#. It defines the signature of methods that can handle a specific event. When an event is raised, the delegate calls all the subscribed event handlers.

    In most cases, the EventHandler delegate is used, which has the following signature:

    public delegate void EventHandler(object sender, EventArgs e);
    

    The sender refers to the object that raised the event, and e contains additional event data (e.g., key presses, mouse positions).

    6. Event Propagation

    Event propagation refers to how events are handled across multiple layers of an application. For example, in GUI applications, events might propagate from a user control (e.g., a button) to its container (e.g., a form), allowing multiple event handlers to handle the event in different places.

    In some systems, events can bubble up or cascade down through the application hierarchy. For example, clicking a button inside a panel might trigger events on the button, the panel, and the form. This mechanism is often called event bubbling or event chaining.

    Workflow of Event-Driven Programming

    Here’s a typical workflow in an event-driven program:

    1. Event Definition: Define the events that the program will respond to (e.g., button click, file download).
    2. Event Source: Create an event source (such as a button) that raises the event when an action occurs.
    3. Event Handler: Create event handler methods that define what should happen when the event is triggered.
    4. Event Subscription: Subscribe to the event using a delegate (method) that will handle the event.
    5. Event Triggering: The event source triggers the event (e.g., a button is clicked).
    6. Event Response: The event handler runs, executing the necessary actions in response to the event.

    Example of Event-Driven Programming in C#

    Let’s look at a simple example of event-driven programming using C#:

    using System;
    
    public class Button
    {
        // Declare an event using the EventHandler delegate
        public event EventHandler Click;
    
        // Method to trigger the event when the button is clicked
        public void OnClick()
        {
            // If there are any subscribers, invoke them
            Click?.Invoke(this, EventArgs.Empty);
        }
    }
    
    public class Program
    {
        // Define an event handler
        private static void ButtonClickHandler(object sender, EventArgs e)
        {
            Console.WriteLine("Button was clicked!");
        }
    
        public static void Main()
        {
            // Create an instance of the Button class
            Button button = new Button();
    
            // Subscribe the event handler to the Click event
            button.Click += ButtonClickHandler;
    
            // Simulate a button click
            button.OnClick();
        }
    }
    

    Output:

    Button was clicked!
    

    Explanation of the Example:

    1. Button Class:

      • The Button class defines an event Click that is triggered when the button is "clicked."
      • The OnClick() method raises the Click event by invoking the Click delegate.
    2. Event Handler:

      • The ButtonClickHandler method is the event handler that will respond when the button is clicked. It prints a message to the console.
    3. Subscribing to the Event:

      • In the Main method, the event handler is subscribed to the Click event of the Button object using the += operator.
    4. Event Triggering:

      • The OnClick() method of the Button class is called, which raises the Click event and invokes the subscribed event handler.

    Characteristics of Event-Driven Programming

    1. Asynchronous: Many event-driven programs are asynchronous, meaning they don't block or wait for one task to finish before starting another. For example, when waiting for a user to click a button, the program doesn't freeze but continues to wait for other events.

    2. Decoupling: Event-driven programming encourages loose coupling between components. The event source (button) doesn’t need to know about the subscribers (handlers). This decoupling makes the code more flexible and easier to maintain.

    3. Non-linear Flow: Unlike traditional sequential programming, where instructions are executed in a linear fashion, event-driven programming allows the program’s flow to be determined by the order of events and the interactions with the user.

    4. Reactivity: The program is reactive rather than proactive. It waits for events (like user input) and responds when they occur. For example, a UI will react to mouse clicks, text inputs, and other actions.

    Use Cases for Event-Driven Programming

    1. GUI Applications: Event-driven programming is the foundation of GUI frameworks like Windows Forms, WPF, and ASP.NET. These applications rely on events to respond to user actions such as clicks, typing, and mouse movement.

    2. Game Development: In games, event-driven programming is used to respond to player actions (e.g., key presses, mouse clicks) and game events (e.g., an enemy is defeated).

    3. Asynchronous Systems: Event-driven programming is ideal for handling asynchronous tasks, like processing network requests, handling file I/O, or responding to user interactions without blocking the main thread.

    4. Server-Side Applications: Many web servers (like ASP.NET Core) use event-driven models to handle incoming requests and events (e.g., HTTP requests, client connections).

    Conclusion

    Event-driven programming is a powerful paradigm that allows programs to react to events, making them dynamic, flexible, and responsive. It is the backbone of modern GUI programming and asynchronous systems. In C#, events and event handlers, coupled with delegates, form the core of event-driven development. By using events effectively, developers can create applications that respond to user input, system notifications, and other asynchronous events in real time.

    Previous topic 2
    Introduction to Events
    Next topic 4
    Message Handling

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,300
      Code examples0
      DifficultyIntermediate