Click here to Skip to main content
15,897,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a timer behind the event raising mechanism?
I'm not sure about some events like Click, MouseDown, ... But if I have a defined event like "Zero" that will fire whenever some property of a control (supports this event) has 0 as its value. The mechanism for this event, I think, is a timer will run and check whenever the property's value is 0, it will fire the "Zero" event. Could you please tell me how the event raising mechanism works?
Thank you so much!
Posted

A good place to read about events is her:

Events (C# Programming Guide)[^]

A good CP article C# Event Implementation Fundamentals, Best Practices and Conventions[^] has help me a lot, in trying to understand events etc.
 
Share this answer
 
 
Share this answer
 
Comments
BobJanova 20-Apr-11 9:45am    
Good link.
Nish Nishant 20-Apr-11 10:07am    
Good link, voted 5, and proposed as answer!
To fire an event, you 'call' it just like a function:

C#
// Declare a delegate
public delegate void MyEventHandler(int EventArg);

// Declare the event.
public event MyEventHandler MyEvent;

// Declare an event handler
public void HandleEvent(int EventArg)
{
    System.Windows.Forms.MessageBox.Show(EventArg.ToString());
}

protected void Button1_Click(object sender, EventArgs e)
{
    // add two handlers... just for fun
    MyEvent += HandleEvent;
    MyEvent += HandleEvent;

    // THIS IS IT!  Raise the event
    MyEvent(123);
}
 
Share this answer
 
Comments
[no name] 20-Apr-11 15:22pm    
I'm sorry but I asked How the event publisher knows if the event happens! For example, when clicking on a button How the button knows it? Or as in my OP, when a property of Myobject is ZERO, how it knows that happens? And I think it should have a built-in timer to check the state of the property. I should say more exactly it is "Data member" not Property, because we can check the changes easily by placing some checking code in the "SETTER" of the property but to check the changes in state of a Data Member is not simple as I thought, It should have a timer to do the check frequently!
I hope you understand what I mean. All you have given I learned months ago!
Thank you!
Whenever you, for example, do a button click, it sends a message to windows. There is a huge list of messages that windows receive for all the operations we do. These messages are tracked and intercepted by .Net code. It is quite similar to you implementing IMessageFilter interface in your code or overriding WndProc method. I do not have visual studio handy but I am quite sure that debugging with "Enable .Net source code debugging" option checked will provide a lot of help.

I simple exercise for you to understand. Just create a blank windows form application and override WndProc method in it. Put a break point and see the values that come in Message parameter. You will be frustrated to never go out of debugging mode unless you put in conditions in your override. :)

The events you would define in your own class will be most of the times dependent on INotifyPropertyChanged interface implementation which already seem to be aware of.
 
Share this answer
 
v2
Comments
BobJanova 20-Apr-11 9:42am    
The message stuff only happens with some events (broadly speaking, those associated with Windows Forms). Events from back end components or called directly from your own classes are run directly, synchronously and within the thread from which they were called – which can be important if you try to do UI updates or something thread-unsafe within the handler.
dan!sh 20-Apr-11 9:48am    
Yes. That's what I had updated. events from your class are often highly dependent on your implementation of INotifyPropertyChanged interface.
BobJanova 20-Apr-11 11:14am    
You can raise events without implementing INotifyPropertyChanged. The only time I use that interface is for data binding.
dan!sh 20-Apr-11 11:16am    
"often highly dependent on" should have been the clue. :)

Good point though. Hope OP gets them and learns.
[no name] 20-Apr-11 15:51pm    
Thank you! A good interface to understand! My 5.004U
Using your example you would do something like this;

C#
//Declare delegate
       public delegate void ZeroEventHandler(object sender, EventArgs e);
       //Declare Event
       public event ZeroEventHandler ZeroEvent;
       //private Field to hold MyNumber value
       public int myNumber = 0;
       public int MyNumber
       {
           get
           {
               return myNumber;
           }
           set
           {
               if (value != myNumber)
               {
                   myNumber = value;
                   if (myNumber == 0)
                   {
                       //Fire Event Here
                       ZeroEvent(this, new EventArgs());
                   }
               }
           }
       }


Hope this helps.
 
Share this answer
 
v2
Comments
[no name] 20-Apr-11 15:48pm    
Yeah! And is this one of advantages of what called PROPERTY? Whenever all events concerning to some property (behind that is some real data member) raise, it should start in the SETTER or GETTER of that property. And about my idea, using a timer to check seems not to be a good way. But what happens if I do this "myNumber = 0" , yes I directly access to the real data member "myNumber" not to start the SETTER, so the ZERO event wouldn't happen even when myNumber is ZERO. To prevent this, we should change the value of the real data member through its corresponding property, shouldn't we?
Thank you so much! You have understood what I want to ask! But about the timer? Does it exist in some case?
Wayne Gaylard 21-Apr-11 2:37am    
That is definitely the advantage of a Property. In my example I mistakenly made the myNumber variable Public, this should be Private so that nobody outside the class can have access to it, thereby forcing the use of the Property MyNumber, thus ensuring the Zero event is always raised. I don't think a timer would be useful in this case as you just need to subscribe to the Zero event, and this will be raised whenever the Property is set to 0.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900