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 );}}}