Let's talk about delegation, events, and Asynchronization in C,
Almost a year has elapsed since I first started c # programming. In the course of learning, there are many things that seem to be the opposite until recently.
This article will first introduce the usage in the evaluation function.
I. Delegation
Basic usage:
1. Declare a delegate type. A delegate is like a 'class'. After a delegate is declared, multiple delegates with this feature can be created. (Feature refers to the return value and parameter type)
public delegate void SomeKindOfDelegate(string result);
2. Create a delegate type created in 1.
public SomeKindOfDelegate aDelegate;
3. Add a response function for the specific delegate created in 2. The response function must conform to the 'feature' in 1 '.
aDelegate +=new SomeKindOfDelegate(aFunctionThatJustForDelegate);private void aFunctionThatJustForDelegate(string result){MessageBox.Show(result);}
4. After completing the preceding three steps, you can use Invoke to call the delegate. Invoke can select the called target function, call priority, and call parameters.
aDelegate.BeginInvoke("Hello~I'm being invoked!", null, null);
The above is the basic usage. In addition to this basic usage, you can also combine var, anonymous delegate, lambda delegate, and other methods.
Complete code:
namespace wtfIsDelegate{ public delegate void SomeKindOfDelegate(string result); public partial class Form1 : Form { public event SomeKindOfDelegate aDelegate; public Form1() { InitializeComponent(); aDelegate +=new SomeKindOfDelegate(aFunctionThatJustForDelegate); aDelegate.BeginInvoke("Hello~I'm being invoked!", null, null); } private void btnDelegate_Click(object sender, EventArgs e) { } private void aFunctionThatJustForDelegate(string result) { MessageBox.Show(result); } }}
Use of delegation:
The advantage of delegation is that it can implement Asynchronization (BeginInvoke) and simplify code when multiple same parameters and return values need to be called at the same time.
------------------------------------------------------------------------
Ii. Events
Basic usage:
1. Define delegation.
public delegate void SomeKindOfDelegate(string result);
2. Define events.
public event SomeKindOfDelegate aDelegate;
3. Add a response function for the event.
process.Exited += new EventHandler(CmdProcess_Exited);
4. Specify the trigger (CALL) method for the event. ([No trigger method, direct invoke ])
Explanation:
In C #, each 'event 'corresponds to its 'event handler eventhandler '. For example, the OutputDataReceived event of the Process class corresponds to DataReceivedEventHandler,
For non-specific 'events', such as PasswordChanged, they all correspond to RoutedEventHandler or EventHandler, which is more common.
'Event processer '. However, 'eventhandler' only acts as an intermediary. We need to manually specify what to do after the 'event' is actually triggered,
Like this:
Process. Exited + = new EventHandler (incluprocess_exited); // register the process Termination event.
EventHandler was originally commissioned. For example
public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);
Custom events
Custom events are similar to delegation,
Custom events change the process of a program in a sense, changing a condition from 'constant query' to 'subscription and process.
Custom events must have the following elements:
Event initiators, event subscriptions, and event handlers. Parameters can be transferred from the initiator to the handler.
The 'initiation' of an event can depend on a certain system message, such as 'onkeydown' and 'onmouseclick' ([I have never seen such a source code yet ]),
It can also be called by itself when a condition is reached (for example, two identical characters are entered) (in fact, receiving a system message is also considered a 'condition fulfillment '). [More events are written in this way]
Some events do not have obvious 'initiators '.
What is the relationship between delegation and events?
------------------------------------------------------------------------
The use of delegation and custom events is very similar. Event can only be Invoke inside the class. If it is delegate, it can be Invoke anywhere. The calling method seems to be slightly different (passing parameters)
The difference between the call method and parameter passing makes the event more conservative/stable. Event is easier to accept from 'comprehension.
Delegate seems to be more used for asynchronous (begin invoke ). Event is more used for custom events.
What is the relationship between delegation and Asynchronization?
------------------------------------------------------------------------
Asynchronization is a function that can be implemented by delegation (or it can be called 'symptom '). Asynchronization can be reflected by many other methods, such as multithreading (thread, threadpool, task, and so on ).