Transmit data to the thread and use the callback method to retrieve data.

Source: Internet
Author: User
Retrieve Data Using callback
/** // * Use the callback method to retrieve data
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;

// The instantiated class used to pass Parameters for operations
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 the 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 ("Starting thread execution ");
// This is to determine whether the thread execution 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 state.
T. Join ();
Console. writeline ("End of thread execution ");
}

// The callback handler should be consistent with the examplecallback parameter type
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 two code examples.
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 instantiated class used to pass Parameters for operations
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 a thread to call threadwithstate
Public Class Example
{
Public static void main ()
{
// Instantiate the threadwithstate class and use the constructor 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 ("Starting thread execution ");
// This is to determine whether the thread execution 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 state.
T. Join ();
Console. writeline ("End of thread execution ");
}
}

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.