Delegates and events in C #

Source: Internet
Author: User

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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.