event handling in c#

Posted on 2011-05-09
Last Modified: 2012-05-11
if i want to do event handling, is there an easier way than to use System.EventHandler and System.EventArgs?  i am just writing an console exe program.  thanks.
Question by:mmingfeilam
    LVL 85

    Accepted Solution

    ...and what events are wanting to trap in your console program?!

    Are you talking about custom events from a class you wrote?  If yes, then you can use any signature you want (even no parameters!).
    LVL 18

    Assisted Solution

    Like this;
    A Timer class for C# Console Applications
    LVL 11

    Assisted Solution

    using System;

    public delegate void DivBySevenHandler(object o, DivBySevenEventArgs e);

    public class DivBySevenEventArgs : EventArgs
        public readonly int TheNumber;
        public DivBySevenEventArgs(int num)
            TheNumber = num;

    public class DivBySevenListener
        public void ShowOnScreen(object o, DivBySevenEventArgs e)
                "divisible by seven event raised!!! the guilty party is {0}",

    public class BusterBoy
        public static event DivBySevenHandler EventSeven;
        public static void Main()
            DivBySevenListener dbsl = new DivBySevenListener();
            EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
        public static void OnEventSeven(DivBySevenEventArgs e)
                EventSeven(new object(),e);
        public static void GenNumbers()
            for(int i=0;i<99;i++)
                    DivBySevenEventArgs e1 = new DivBySevenEventArgs(i);

    more on this url
    LVL 29

    Assisted Solution

    I believe your  requirements are not quite clear.

    Really, I believe guys have answered the question already. I.e., Idle_Mind answered.

    Yes, you may have an event with any signature. No any arguments  - is OK.

    However, I can't see any problems with arguments. Moreover, the standard signature for .Net events is:

    (object sender, EventArgs e)

    e - completely useless object. It contains NO information whatsoever.
    sender - may not be used at all

    However, this signature is a standard. Now I work with a big application. Some developers some time ago created events with no arguments. Now some additional information is required to be passed - custom event arguments are needed (that inherit from EvenArgs). But I also need to go through code and change signature everywhere. If created handlers had standard signature - I might have much less work. Can't repeat here words I'm telling to these lazy developers... :)

    My advice: use standard signatures. It won't save you much time if you skip standard arguments...:)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now