.Net edge learns an edge to tell (three)
Tuesday, March 03, 2009 by rain
If you had used C,You should know to a function calls Qsort,Be those who use sort.But the comparison that this function cannot assume extensive meaning to go up apparently,Accordingly you need to deliver a finger,He is pointed to have the function that compares a function.Qsort should transfer this function when every secondary compares array element.The concept that this is Callback,Also can realize a callback in.Net,The method is to found an interface,Come true he,The adduction that delivers a target that achieves this interface.Delegate,The function guiding principle that you can understand him a safety.
Notifications has a place with Callback similar,But want than simple callback complex much.The establishs Callback method that while Callback means the Callback method that should call to be called, should call.This is a very close together coupling.And Notification wants a few more inattentive,You can register the Notification that some paragraph of time is met in the future or won't happen,Handle when they happen only,Otherwise need not.
The package that you perhaps consider to let you write informs other component when a few things happen,For example,You want to write package of a pushbutton,When your Click when you may want to inform other component,And other component will must prepare to request Notification to you,You are about to offer a method to tell them you had had Notification of a practicable.On the other hand,You also may be the person that is informed when the hope when a few things of other component happen.At this moment you find that specific component to be able to offer what Notification with respect to need.
In.Net Event is you use be on the air, cause, the mechanism that handles Notification.It is roughly such,The component that can cause incident states this one incident.And hope processing some is specific the component of specific event passes the some of package the Delegate that delivers a method is registered to the component that causes incident.Such,The method that causes the component of incident to be able to call every to already was registered when incident happening.The function that through Delegate and Event we can realize asynchronous to call.Such statement are in C# of a representative:When representative of Public Delegate Void LogHandler(String Message); is handling this kind of callback, had been very powerful.But so that,when us need representative is stored the following Notification,Had a bit of trouble.Have an object Button e.g. us,Have event of a Click.We can state the acting type of a ClickHandler is used at handling Click incident,State in the Class of our Button the Public example of a ClickHandler,When so other component hopes Click happens, be informed,In adding Click to act as agent with respect to the OK and simple representative him, go.MyButton.Click += New ClickHandler(MyMethod);
Looking to be like problem of it doesn't matter.But here is put in a big question however,We state Click acts as agent is Public,The Data Fields that this disobeyed us to had said before does not state forever into Public,This meeting has a series of troubles.Settlement way is statement into Private or Protected,Solve with attribute next read write.Such we are OK Private statement Click,Writing method of a pair of Public to increase a Listener to reach reduce a Listener.It is of course in.Net,State when you when an Event,.Net had done good all these for you.State an incident:
Class AlarmTimer {
Public Event EventHandler Alarm;
/ / . . .
}
AlarmTimer of specification of this paragraph of code can broadcast to all other object the incident that it can cause to call Alarm.What Alarm incident uses is EventHandler representative type.EventHandler:Without return of value, accept two parameter (Object: 脂E of Lu of camel of ⑺ of Mu of of the collection that drive the larva of a tapeworm or the cercaria of a schistosome VentArgs:Include the data about incident)
See a case:
Class AlarmTimer {
Public Event EventHandler Alarm;
Private Timer MyTimer;
Public AlarmTimer() {
MyTimer = New Timer();
MyTimer.Tick += New EventHandler(OnTick);
}
Public Void Set(Double Seconds) {
MyTimer.Interval = (Int32)(seconds * 1000);
MyTimer.Start();
}
Protected Void OnTick(Object Sender, eventArgs E) {
MyTimer.Stop();
If (Alarm! = Null) Alarm(this, eventArgs.Empty);
}
Public Void ReEnable() {
MyTimer.Enabled = True;
}
}
Notice AlarmTimer causes incident already (Alarm) handle incident again (the Tick incident in Timer)
Static AlarmTimer MyAlarm = New AlarmTimer();
Public Static Void TestEvent() {
MyAlarm.Alarm += New EventHandler(TimerEventProcessor);
MyAlarm.Set(2);
Console.WriteLine("Timer Is Set; Alarm Will Go Off In Two Seconds" );
Application.Run();
}
/ / handle incident
Private Static Void TimerEventProcessor(
Object MyObject, eventArgs MyEventArgs) {
If (MessageBox.Show("Wake Up! Continue Ringing? ",
"Count Is: "+ AlarmCounter,
MessageBox.YesNo)==DialogResult.Yes) {
AlarmCounter += 1;
MyAlarm.ReEnable();
}
Else {
Application.Exit();
}
}
Notice:Object and EventArgs are not indispensible parameter,Just this is a good mode that write a law just
About Event a lot of have not say,After staying, say slowly.