Main Site Documentation

How to use event effective?


#1

I don’t know the real usage and basis of the Event.
For example,
Where the event should be initialized? always put in ProgramStarted()?
Does the order to event is important ? Case1 equals to Case2?
Case1"
Event1 += …
Event2 +=…
Case2:
Event2 +=…
Event1 +=…

How to control the time of event?
For example, I have a ENC28 module and a camera module.
When the networks up , I will take a picture.
But, the camera has an event camera.CameraConnected, which is told me when will the camera is already to use.

So, is it possible that the networks up but the camera.CameraConnected not fired yet?

Does anyone can help me to figure out it or give me some reference which I can learn for event use?

Thanks


#2

The attaching of an event handler (your code) to an event can be done in any order. In a Gadgeteer app, ProgramStarted is about the most correct place to add it. So Case 1 and 2 are the same.

You need to manage the state of all things - so in your network connected/camera connected example, you need to know that both have fired and check appropriately before you take an action that assumes something is working.

I always think of events more for autonomous state changes, and less on a sequence of state changes - as soon as you have a sequence you are really talking about needing a state machine of some kind “orchestrating” everything.


#3

Here are two more detailed explanations of delegates and events in .Net

http://csharpindepth.com/Articles/Chapter2/Events.aspx

http://www.codeproject.com/Articles/20550/C-Event-Implementation-Fundamentals-Best-Practices