C # 2.0 Anonymous methods are called asynchronously with Windows Forms

Source: Internet
Author: User
Tags anonymous thread
window| Asynchronous C # 2.0 anonymous methods greatly simplifies the implementation of the Windows Forms asynchronous invocation, and we no longer have to manually define asynchronous delegates or wrapper classes. For example, in the following code example, Form1 has a button control and a list control, in the Click event of a button control, we create a new thread that adds 10 items to the list control in this thread:

public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.ListBox ListBox1;
Private System.Windows.Forms.Button button1;

...

private void Button1_Click (object sender, EventArgs e)
{
Thread thread = new Thread (this. THREADPROC);
Thread. Start ();
}

private void ThreadProc ()
{
for (int i = 0; i < i++)
{
This. Invoke ((MethodInvoker) delegate
{
THIS.LISTBOX1.ITEMS.ADD ("Item" + (i + 1). ToString ());
});
}
}
}





Related Article

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.