from:http://blog.csdn.net/xuwei_xuwei/article/details/18008013
Quan
For invokerequired, the video can be used to determine if it is required, and if there is a possibility for multiple threads to access it, then it is not necessary to do so if only the thread is accessible.
---------------
The following narrative is personal understanding:
Definition understanding
Delegate: Using formal and class methods similar, essentially a class, compiler compile time, it will be compiled into the corresponding class, parameters can be the method name,
Event: is a response to a user action, the average user has an action, triggered an event, of course, in C # we can customize their own events, and then we need to program the trigger,
Relationship of delegates and events
An event requires a delegate to sign, and one of the purposes of the delegate is to sign the event,
Use
Delegate: There are three main purposes for signing events and invoking synchronous methods across threads using controls and asynchronously
cross-thread invocation example
Declaring a delegate
Private delegate void Appendtextdel (textbox textbox, string content);
public void AppendText (textbox textbox, string content)
{
if (textbox.invokerequired)//Determine whether a textBox is called across threads
{
Appendtextdel append = new Appendtextdel (appendtext);
Textbox.invoke (Append, TextBox, content);
}
Else
{
TextBox.Text = content + "\ r \ n" + textbox.text;//append text and line wrap
}
}
When you need to assign a value to a control of the textbox type, the call can
AppendText (TextBox1, "Hello World")
Example of an event signature
public delegate void Eventdel ();//Declaration Delegate
Public event Eventdel customevent;//declares a custom event, with a Eventdel delegate signature (in other words, this can only be called and Eventdel Delegate matching method)
Defines a method that a Eventdel can invoke
public void Methonda () {
Console.WriteLine ("Methonda");
}
Defines a method that a Eventdel can invoke
public void Methondb () {
Console.WriteLine ("Methondb");
}
Test
public void Testmethond () {
This. Customevent + = new Eventdel (Methonda);
This. Customevent + = new Eventdel (methondb);
Customevent ();//Contact Custom Event
}
Example of asynchronous method calling synchronous methods
(specifically the invocation of the delegate's BeginInvoke and EndInvoke to implement, see Code)
delegate void Messagedel ();
public void Custommessage () {
Console.WriteLine ("Synchronous method called asynchronously");
}
To invoke asynchronously, the following
Messagedel del = new Messagedel (custommessage);
Del. BeginInvoke (Asynvcallback delegate object, user-defined object type);
Or
Messagedel del = new Messagedel (custommessage);
IAsyncResult result = Del. BeginInvoke ();
...//By using result for subsequent processing, both methods of invocation are OK
Delegates and events in C #