Click here to Skip to main content
15,917,808 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to break from an If Statement Pin
Guffa30-Aug-06 19:09
Guffa30-Aug-06 19:09 
GeneralRe: How to break from an If Statement Pin
_AK_30-Aug-06 19:56
_AK_30-Aug-06 19:56 
AnswerRe: How to break from an If Statement Pin
Guffa30-Aug-06 21:13
Guffa30-Aug-06 21:13 
AnswerRe: How to break from an If Statement Pin
Pooja k30-Aug-06 19:25
Pooja k30-Aug-06 19:25 
GeneralRe: How to break from an If Statement Pin
Guffa30-Aug-06 19:46
Guffa30-Aug-06 19:46 
AnswerRe: How to break from an If Statement Pin
Pooja k30-Aug-06 20:00
Pooja k30-Aug-06 20:00 
AnswerRe: How to break from an If Statement Pin
Martin#30-Aug-06 20:47
Martin#30-Aug-06 20:47 
Questionwhy do the custom written events NEED to have a client?? Pin
michal.kreslik30-Aug-06 17:44
michal.kreslik30-Aug-06 17:44 
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."); 
        } 
    } 
} 


C# source and compiled exe attached below.

Any comments on this weird behavior?

Michal

Seems like it's not possible to attach a file here, so you can download the .cs file here: http://kreslik.com/forums/viewtopic.php?t=231[^]
AnswerRe: why do the custom written events NEED to have a client?? Pin
Qhalis30-Aug-06 19:05
Qhalis30-Aug-06 19:05 
GeneralRe: why do the custom written events NEED to have a client?? Pin
michal.kreslik30-Aug-06 20:07
michal.kreslik30-Aug-06 20:07 
GeneralRe: why do the custom written events NEED to have a client?? Pin
mav.northwind30-Aug-06 20:19
mav.northwind30-Aug-06 20:19 
Questionmulticolumn combobox Pin
Amar Chaudhary30-Aug-06 16:36
Amar Chaudhary30-Aug-06 16:36 
AnswerRe: multicolumn combobox Pin
Nader Elshehabi31-Aug-06 1:43
Nader Elshehabi31-Aug-06 1:43 
GeneralRe: multicolumn combobox Pin
Amar Chaudhary31-Aug-06 15:28
Amar Chaudhary31-Aug-06 15:28 
QuestionDataSets Modifying Data Pin
Expert Coming30-Aug-06 16:03
Expert Coming30-Aug-06 16:03 
AnswerRe: DataSets Modifying Data Pin
Muammar©10-Sep-06 0:31
Muammar©10-Sep-06 0:31 
QuestionMatlab to C# Pin
signimage30-Aug-06 16:02
signimage30-Aug-06 16:02 
AnswerRe: Matlab to C# Pin
Expert Coming30-Aug-06 16:06
Expert Coming30-Aug-06 16:06 
Questioncrestal report Pin
TAREQ F ABUZUHRI30-Aug-06 13:59
TAREQ F ABUZUHRI30-Aug-06 13:59 
AnswerRe: crestal report Pin
Judah Gabriel Himango30-Aug-06 14:08
sponsorJudah Gabriel Himango30-Aug-06 14:08 
GeneralRe: crestal report Pin
TAREQ F ABUZUHRI30-Aug-06 21:18
TAREQ F ABUZUHRI30-Aug-06 21:18 
GeneralRe: crestal report Pin
gus_br31-Aug-06 7:36
gus_br31-Aug-06 7:36 
AnswerRe: crestal report Pin
gus_br30-Aug-06 16:09
gus_br30-Aug-06 16:09 
AnswerRe: crestal report Pin
Glen Harvy31-Aug-06 13:10
Glen Harvy31-Aug-06 13:10 
GeneralRe: crestal report Pin
gus_br31-Aug-06 15:36
gus_br31-Aug-06 15:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.