ASP. NET (a series of learning languages)-multithreading programming in. NET-passing Parameters Using Constructors (III)

Source: Internet
Author: User

  Preface

As mentioned in the previous section, the parameter for creating a thread is a function. When a new thread starts, it will execute this function without any parameters, if there is no return value, how can we call a function with parameters?

  Reading directory

I. Passing Parameters Using Constructors

Ii. Operating Mechanism

Iii. Running Effect

  I. Passing Parameters Using Constructors

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading;

Namespace _ 2_ThreadArgs
{
Class Program
{
Static void Main (string [] args)
{
// Parameters passed by the constructor
User user = new User ("getting sleepy since childhood ");
Thread thread = new Thread (new ThreadStart (user. GetName ));
Thread. Start ();
Thread. Join (Timeout. Infinite );
}
}

/// <Summary>
/// User class
/// </Summary>
Class User
{
Private string _ name;
Public User (string strName)
{
_ Name = strName;
}

Public void GetName ()
{
Console. WriteLine ("My name is:" + _ name );
}

}
}

  Ii. Operating Mechanism

First, we construct a class User with parameters and input a string. Then, the class is saved in a class member variable through the constructor, and there is also a GetName () the method is to output a sentence. First, we construct a class, and then use the GetName () method of this class as the ThreadStart () parameter, that is, using the constructor to pass the Parameter

  Iii. Running Effect

  

 

 

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.