C # multithreaded programming, parameters, accepting return values

Source: Internet
Author: User

Today will be multi-threaded knowledge has reviewed the next, summed up a few points:

Create a new thread (no parameters, no return value)

    1. Thread th = new Thread (new ThreadStart (Printname));
    2. public void Printname () //function
    3. {
    4. //function Body
    5. }

It is important to note that the functions in ThreadStart are not return values and parameters.

Then there are parameters, which are as follows:

    1. Thread th = new Thread (new Parameterizedthreadstart (Printname));
    2. Public void Printname (string name) //function
    3. {
    4. //function Body
    5. }

If you encounter and need a return value, and you need parameters, you can consider using async:

But we need to declare a commission.

  1. Public Delegate string Methodcaller (string name); Define an agent
  2. Methodcaller mc = New Methodcaller (GetName);
  3. String name = "My name"; Input parameters
  4. IAsyncResult result = MC. BeginInvoke (name,null, null);
  5. String myname = mc. EndInvoke (result); //To receive return values
  6. Public string GetName (string name) //function
  7. {
  8. return name;
  9. }

Note here that the generation of new threads in this way is running in the background (background), with the priority being normal

C # multithreaded programming, parameters, accepting return values

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.