Multi-thread example, data transfer | software development | transferred from blog
Source: Internet
Author: User
Retrieve Data Using callback
The following example demonstrates a callback Method for retrieving data from a thread. Constructor of classes containing 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;
// Instantiated class used to pass Parameters for operation
Public Class Threadwithstate
{
Private String Boilerplate;
Private Int Value;
// Define a callback function
Private Examplecallback callback;
// Parameters passed by the receiving thread
Public Threadwithstate ( String Text, Int Number, examplecallback callbackdelegate)
{
Boilerplate=Text;
Value=Number;
Callback=Callbackdelegate;
}
Public Void Threadproc ()
{
Console. writeline (boilerplate, value );
//Determine whether to execute the callback function and pass the parameter to the callback function.
If(Callback! = Null)
Callback (1);
}
}
// Declare a callback function: note that the passed parameters must be of the same type as the function parameters in the example class.
Public Delegate Void Examplecallback ( Int Linecount );
// Case Master Class
Public Class Example
{
Public Static Void Main ()
{
// Instantiate threadwithstate and PASS Parameters. Note: The namespace reference of New examplecallback (resultcallback) and the passed parameters (resultcallback function)
Threadwithstate TWS = New Threadwithstate ( " This number {0 }. " , 42 , New Examplecallback (resultcallback ));
Thread t = New Thread ( New Threadstart (TWS. threadproc ));
T. Start ();
Console. writeline ( " Thread execution started " );
// This is to determine whether the thread is finished, but it is not recommended to block the thread, so that the thread will wait for completion before other operations
// You can use t. isalive to determine whether the thread is in the execution status.
T. Join ();
Console. writeline ( " Thread execution ends " );
}
// The callback handler should be consistent with the parameter type of examplecallback.
Public Static Void Resultcallback ( Int Linecount)
{
Console. writeline ("Printed {0} lines.", Linecount );
}
}
Transmit data to the thread
/**/ /*Transmit data to the thread
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;
// Instantiated class used to pass Parameters for operation
Public Class Threadwithstate
{
Private String Boilerplate;
Private Int Value;
Public Threadwithstate ( String Text, Int Number)
{
Boilerplate=Text;
Value=Number;
}
Public Void Threadproc ()
{
Console. writeline (boilerplate, value );
}
}
// Create thread call threadwithstate
Public Class Example
{
Public Static Void Main ()
{
// Instantiation class threadwithstate, using constructors to pass parameters to the thread
Threadwithstate TWS = New Threadwithstate ( " This number {0 }. " , 42 );
// Create a thread and execute the threadproc function in the threadwithstate class.
Thread t = New Thread ( New Threadstart (TWS. threadproc ));
T. Start ();
Console. writeline ( " Thread execution started " );
// This is to determine whether the thread is finished, but it is not recommended to block the thread, so that the thread will wait for completion before other operations
// You can use t. isalive to determine whether the thread is in the execution status.
T. Join ();
Console. writeline ( " Thread execution ends " );
}
}
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