The threadstart delegate has neither parameters nor return values. This means that the thread cannot be started using the method that requires parameters, or the return value can be obtained from the method.
To pass data to a thread, you need to create an object to maintain data and thread methods, as shown in the following twoCodeExample.
To retrieve the results of the thread method, you can use the callback method, as shown in the second code example.
Using system;
Using system. Threading;
// The threadwithstate class contains the information needed
// A task, and the method that executes the task.
//
Public class threadwithstate {
// State information used in the task.
Private string boilerplate;
Private int value;
// The constructor obtains the state information.
Public threadwithstate (string text, int number ){
Boilerplate = text;
Value = number;
}
// The thread procedure performs the task, such as formatting
// And printing a document.
Public void threadproc (){
Console. writeline (boilerplate, value );
}
}
// Entry point for the example.
//
Public class example {
Public static void main (){
// Supply the state information required by the task.
Threadwithstate TWS =
New threadwithstate ("This report displays the number {0}.", 42 );
// Create a thread to execute the task, and then
// Start the thread.
Thread t = new thread (New threadstart (TWS. threadproc ));
T. Start ();
Console. writeline ("main thread does some work, then waits .");
T. Join ();
Console. writeline ("independent task has completed; main thread ends .");
}
}
Retrieve Data Using callback
The following example demonstrates a callback Method for retrieving data from a thread. The constructor of classes that contain data and thread methods also accepts the delegate representing the callback method. Before the thread method ends, it calls the callback delegate.
Using system;
Using system. Threading;
// The threadwithstate class contains the information needed
// A task, the method that executes the task, and a delegate
// To call when the task is complete.
//
Public class threadwithstate {
// State information used in the task.
Private string boilerplate;
Private int value;
// Delegate used to execute the callback method when
// Task is complete.
Private examplecallback callback;
// The constructor obtains the State information and
// Callback delegate.
Public threadwithstate (string text, int number,
Examplecallback callbackdelegate)
{
Boilerplate = text;
Value = number;
Callback = callbackdelegate;
}
// The thread procedure performs the task, such
// Formatting and printing a document, and then invokes
// The callback delegate with the number of lines printed.
Public void threadproc (){
Console. writeline (boilerplate, value );
If (callback! = NULL)
Callback (1 );
}
}
// Delegate that defines the signature for the callback method.
//
Public Delegate void examplecallback (INT linecount );
// entry point for the example.
//
public class example {
Public static void main () {
// supply the state information required by the task.
threadwithstate TWS = new threadwithstate (
"This report displays the number {0 }. ",
42,
New examplecallback (resultcallback)
);
Thread t = new thread (New threadstart (TWS. threadproc ));
T. Start ();
Console. writeline ("main thread does some work, then waits .");
T. Join ();
Console. writeline ("independent task has completed; main thread ends .");
}
// The callback method must match the signature of
// Callback delegate.
//
Public static void resultcallback (INT linecount ){
Console. writeline ("independent task printed {0} lines.", linecount );
}
}