Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 331589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:47:18+00:00 2026-05-12T09:47:18+00:00

See Also: understanding events and event handlers in C# As a web dev, I

  • 0

See Also:

understanding events and event handlers in C#

As a web dev, I don’t typically have to get into building my own events for objects, since most are inherent in the page. However, now I’m into a pretty complex (web) project where I think I may have to use them. I just read a chapter out of Pro VB 2008 and the .NET 3.5 Platform regarding events, delegates and lambdas (ch. 11) and while I have a better idea of what’s going on, I am still having trouble wrapping my mind around when exactly to use them and how to use them. I understand their implementation, but the example in the book is a bit contrived.

I was hoping someone with a bit more understanding on the subject of events could provide me with a really solid example.

For my real-world application, I’m trying to use an event that would let me know when an item (some class, named OrderItem) has been updated and perform actions based on whether or not it’s been updated. I considered using a flag/boolean property, but I know this doesn’t smell right, and it’s high-time I learn about how to use events correctly.

Thanks a lot!

EDIT Ok, so I guess to clarify a bit, what is the point of calling an event when all it is doing is calling a method? Why not simply call the method? This isn’t a duplicate either, as I’m talking conceptually and she wants to know about handlers specifically. Also, I want to know what the difference would be between using an event or a "flag" property. And what do people mean by "subscribe" to an event?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:47:18+00:00Added an answer on May 12, 2026 at 9:47 am

    Events are a specific case of the Inversion of Control (IoC) pattern. The traditional control flow, the caller invokes a method on the callee (like you are suggesting). With IoC (and thus events), we change around the application control and instead of tell the callee what to do, we tell the callee to notify us when something we are interested in happens.

    Consider the case of a Timer. I want to be notified every 5 seconds (say). I don’t want to constantly poll the timer (“is it time yet? is it time yet? is it time yet?”). Instead, I want to invert the flow control of my application. I want to tell the timer: “When it’s time, please tell me by calling this method.” That way, control is “inverted”, the callee is invoking a method on the caller!

    Consider the following code …

    using System;
    using System.Timers;
    
    namespace TestTimer
    {
        class Program
        {
            static void Main(string[] args)
            {
                // create my timer.
                var t = new System.Timers.Timer(1000);
    
                // register for notification
                // tell the timer, "when it's time, call TimerGoesOff method"
                t.Elapsed += new ElapsedEventHandler( TimerGoesOff );
    
                // start the timer
                t.Start();
    
                // wait
                Console.ReadLine();
            }
    
            // this gets called when the timer goes off
            public static void TimerGoesOff(object source, ElapsedEventArgs e)
            {
                Console.WriteLine("The Time is " + e.SignalTime);
            }
        }
    }
    

    Rather than call a method on the Timer to ask when it will go off again (as you suggest), I tell it, “when you go off, call the TimerGoesOff” method. Now, instead of just waiting for the Timer to go off, I could do some work. Consider this code …

    using System;
    using System.Threading;
    using System.Timers;
    
    namespace TestTimer
    {
        class Program
        {
            static void Main(string[] args)
            {
                // create my timer.
                var t = new System.Timers.Timer(1000);
    
                // register for notification
                t.Elapsed += new ElapsedEventHandler( TimerGoesOff );
    
                // start the timer
                t.Start();
    
                // do some work while timer is going
                new Thread(() => DoWork()).Start();
    
                Console.ReadLine();
            }
    
            // this gets called when the timer goes off
            public static void TimerGoesOff(object source, ElapsedEventArgs e)
            {
                Console.WriteLine("The Time is " + e.SignalTime);
            }
    
            public static void DoWork()
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine( "Working ..." );
                    Thread.Sleep( 1000 );
                }
            }
        }
    }
    

    Now, I get output something like …

    Working ...
    The Time is 8/25/2009 1:05:59 PM
    Working ...
    Working ...
    The Time is 8/25/2009 1:06:00 PM
    Working ...
    The Time is 8/25/2009 1:06:01 PM
    Working ...
    The Time is 8/25/2009 1:06:02 PM
    The Time is 8/25/2009 1:06:03 PM
    

    I have used the Timer.Elapsed Event to change the control flow of my application. I can go off and do work while “waiting” for the timer event to pulse. This is made possible by IoC and Events.

    This is particularly visible (hehe) in User Interfaces. I don’t want to keep asking “has the user done anything yet? has the user done anything yet?” (that’s the way it used to be done way back when). Instead, I tell Controls for example, “when the user clicks you, let me know”. That way, I can go off and do other great stuff and still be responsive to the user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have real understanding of the MVC model or Architecture yet but what
I would like to see how Web Developers avoid the double submission problem. So
This is a new understanding of a question I posted previously: I have a
So I want to get an understanding of how a carousel works, and what
I'm learning MEF and have some problems with understanding it. I have small project
I know that automatic properties must define a get and set accessor method, I
I have studied that during a fork, the data and code segment of the
I am not sure if I am using this correctly so my problem may
In the book The Algorithm Design Manual by Skiena, computing the mode (most frequent
Let's say I'm querying an xhtml document, and I want to query all of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.