Original address: Https://msdn.microsoft.com/zh-cn/library/ts553s52 (v=vs.110). aspx
passing data to line Rountines accesses retrieving data from a thread
In the. NET Framework version 2.0, theParameterizedthreadstart delegate provides an easy way to invoke the Thread. when the Start method overloads, the object that contains the data is passed to the thread. For a code example, see Parameterizedthreadstart.
UseThe Parameterizedthreadstart delegate is not a type-safe method of passing data because of Thread. The Start method overload accepts any object. an alternative is to encapsulate the threading process and data in the helper class and use the ThreadStart delegate to execute the thread procedure. This technique is demonstrated in the following two code examples.
Neither of these delegates has a return value because there is no place to return data from an asynchronous call. to retrieve the results of a threading method, you can use a callback method, as shown in the second code example.
usingSystem;usingSystem.Threading;//The Threadwithstate class contains the information needed for//a task, and the method that executes the task.// helper class Public classthreadwithstate{//State information used in the task. Private stringboilerplate; Private intvalue; //The constructor obtains the state information. PublicThreadwithstate (stringTextintNumber ) {Boilerplate=text; Value=Number ; } //the thread procedure performs the task, such as formatting//and printing a document. Public voidThreadProc () {Console.WriteLine (boilerplate, value); }}//Entry point for the example.// Public classexample{ Public Static voidMain () {//supply The state information required by the task.Threadwithstate TWS =NewThreadwithstate ("This report displays the number {0}.", the); //Create A thread to execute the task, and then//start the thread.Thread T =NewThread (NewThreadStart (TWS). ThreadProc)); T.start (); Console.WriteLine ("Main thread does some work and then waits."); T.join (); Console.WriteLine ("independent task has completed; main thread ends."); }}//The example displays the following output://Main thread does some work and then waits.//This is the number of displays.//independent task has completed; main thread ends.
Retrieving data using callback methods
The following example shows a callback method that retrieves data from a thread. The constructor of the class that contains the data and threading methods also accepts the delegate that represents the callback method, and it invokes the callback delegate before the thread method finishes.
usingSystem;usingSystem.Threading;//The Threadwithstate class contains the information needed for//a task, the method that executes the task, and a delegate//To call when the task was complete .// Public classthreadwithstate{//State information used in the task. Private stringboilerplate; Private intvalue; //Delegate used to execute the callback method when the//task is complete. PrivateExamplecallback callback; //The constructor obtains the state information and the//callback delegate. PublicThreadwithstate (stringTextintNumber , Examplecallback callbackdelegate) {Boilerplate=text; Value=Number ; Callback=callbackdelegate; } //the thread procedure performs the task, such as//formatting and printing a document, and then invokes//The callback delegate with the number of lines printed. Public voidThreadProc () {Console.WriteLine (boilerplate, value); if(Callback! =NULL) Callback (1); }}//Delegate that defines the signature for the callback method.// Public Delegate voidExamplecallback (intlinecount);//Entry point for the example.// Public classExample { Public Static voidMain () {//supply The state information required by the task.Threadwithstate TWS =NewThreadwithstate ("This report displays the number {0}.", the, NewExamplecallback (resultcallback)); Thread T=NewThread (NewThreadStart (TWS). ThreadProc)); T.start (); Console.WriteLine ("Main thread does some work and then waits."); T.join (); Console.WriteLine ("independent task has completed; main thread ends."); } //The callback method must match the signature of the//callback delegate. // Public Static voidResultcallback (intlinecount) {Console.WriteLine ("Independent task printed {0} lines.", LineCount); }}//The example displays the following output://Main thread does some work and then waits.//This is the number of displays.//Independent task printed 1 lines.//independent task has completed; main thread ends.
Create threads at startup and pass data