Switch:. Net thread call method (no parameters and parameters)

Source: Internet
Author: User
Tags net thread
. Net thread call method (no parameters and parameters)

1. Call a method without Parameters

Public class Program
{
Public static void process ()
{
//......
}
}

Thread thread = new thread (New threadstart (threading. Process ));
Thread. Start ();

2. Call a method with Parameters(. NET specifies that this method has only one parameter and must be of the object type)

Public class Program
{
Public static void process (Object Data)
{
//......
}
}

List <string> DATA = new list <string> ();
Data. Add (" ");

Thread thread = new thread (New parameterizedthreadstart (parameters. Process ));
Thread. Start (data );

3. Call multiple parameters(With lambda expressions in. Net 3.5, you can easily call them)

Public class Program
{
Public static void process (string P1, int P2)
{
//......
}
}

Thread thread = new thread () => {process. Process ("", 888 );});
Thread. Start ();

The compiler converts a Lambda expression to an instance of the corresponding delegate type and generates a random method. We know that the thread constructor has two Delegate types: threadstart and parameterizedthreadstart, if it converts a Lambda expression to a threadstart instance, the compiler will compile it as follows:Code.

Thread thread = new thread (New threadstart (TS); // generated by the compiler
Thread. Start ();

Threadstart Ts = new threadstart (isvalidxxxx); // generated by the compiler

Public static void isvalidxxxx () // generated by the compiler
{
Worker. Process ("Jianghu kiddies", 888 );
}

Note: The names of new variables or methods generated by the compiler are random.

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.