C # input values and return values between multiple threads

Source: Internet
Author: User

I don't like to talk so much nonsense. I can directly access the code without reading comments! Standing in front of the Code, all problems will no longer be a problem!

1. threads without Parameters

Class main
{
Public static void main (string [] args)
{
Thread th1 = new Thread (DoWork );
Th1.Start ();

Thread Th1 = new Thread (new ParameterizedThreadStart (DoWorkWithParam ));
Object data = null;
Th2.Start (data );
}

Private void DoWork ()
{
// Do something
}

Private void DoWorkWithParam (object param)
{
String data = param. ToString ();
// Do something
}

}

2. Include a parameter

Class main
{
Public static void main (string [] args)
{
Thread Th1 = new Thread (new ParameterizedThreadStart (DoWorkWithParam ));
Object data = null;
Th2.Start (data );
}

Private void DoWorkWithParam (object param)
{
String data = param. ToString ();
// Do something
}

}

3. Two or more parameters
Public class ThreadWithParams {
Private string param1;
Private int param2;

Public ThreadWithParams (string param1, int param2)
{
This. param1 = param1;
This. param2 = param2;
}

Public void ThreadProc ()
{
Console. WriteLine (param1, param2 );
}
}

Public class Main {
Public static void Main ()
{// Initialize Two Parameters
ThreadWithParams twp = new ThreadWithState ("I am param1", 2 );
Thread t = new Thread (new ThreadStart (twp. ThreadProc ));
T. Start ();
}
}

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.