C # multi-thread programming-transfer data to a thread

Source: Internet
Author: User

There are two ways to transmit data to a thread:

    • Use the threadstart method with Parameters
    • Create a custom class and define the thread method as the instance method. In this way, you can initialize the instance data and then start the thread.
 Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Threading; // Transmit data to the thread  // Herbert  // For personal learning only  Namespace Threaddemo { Class Program { Public   Struct Data {Public   String Message ;} Static   Void Threadmainwithparameters ( Object O) {data d = (data) O; console. writeline ( "Running in a thread, received {0 }" , D. Message );} Static   Void Main ( String [] ARGs) {data d = New Data (); D. Message = "Info" ; Thread t2 = New Thread (threadmainwithparameters); t2.start (d); mythread OBJ = New Mythread ( "OBJ" ); Thread T3 = New Thread (obj. threadmain); t3.start (); console. readkey () ;}} mythread class: Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Namespace Threaddemo { Class Mythread { Private   String Data;Public Mythread ( String Data ){ This . Data = data ;} Public   Void Threadmain () {console. writeline ( "Running in a thread, data: {0 }" , Data );}}}

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.