C # Two Methods for passing data to a thread [C # advanced tutorial]

Source: Internet
Author: User

1. a class that uses methods to transmit data to a thread.

 

// Data structure to be transmitted <br/> public struct data <br/>{< br/> Public String message; <br/>}</P> <p> // method of specific functions. Note that the object type <br/> static void threadmainwithupa (Object O) <br/>{< br/> data d = (data) O; <br/> console. writeline (D. message); <br/>}</P> <p> private void form1_load (Object sender, eventargs E) <br/>{< br/> // call <br/> data d = new data (); <br/> D. message = "OK message"; // assign a value to the data structure <br/> thread T1 = new thread (threadmainwithupa); <br/> t1.start (d); <br/>

 

 

2. If you are not too troublesome or have special requirements, you can also create a new class to provide data to the thread.

 

 

// This is a new class <br/> public class mythread <br/>{< br/> // The data to be uploaded, the class has become an attribute <br/> private string data; </P> <p> // constructor, no return value <br/> Public mythread (string data) <br/>{< br/> This. data = data; <br/>}</P> <p> // print and implement functions <br/> Public void printmess () <br/> {<br/> console. writeline ("The message is:" + data ); <br/>}< br/> // ======================== ======================================< br/> // load events in another class </P> <p> private void form1_load (Object sender, eventargs e) <br/>{< br/> // instantiate this class <br/> mythread my = new mythread ("babababa "); </P> <p> // open the thread. The parameter is the real method in this class. <br/> thread t2 = new thread (my. printmess); <br/> t2.start (); <br/>}</P> <p>

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.