Part 89 to 91 Talking about pass the parameters in thread, talkingparameters

Source: Internet
Author: User

Part 89 to 91 Talking about pass the parameters in thread, talkingparameters

Part 89 ParameterizedThreadStart delegate

Use ParameterizedThreadStart delegate to pass data to the thread function

Class Program {static void Main (string [] args) {Console. writeLine ("input a target number:"); object target = Console. readLine (); ParameterizedThreadStart pt = new ParameterizedThreadStart (Number. print); // really need to write that? No, you can just pass the function into Thread class. thread t = new Thread (pt); // new Thread (Number. print); t. start (target); Console. readLine () ;}} public class Number {public static void Print (object target) {int number = 0; if (int. tryParse (target. toString (), out number) {for (int I = 0; I <number; I ++) {Console. writeLine (I );}}}}View Code

How we are not explictly creating an instance of ParameterizedThreadStart delegate. Then how is it working?

It's working because, the compiler implicitly converts new Thread (Number. Print); to new Thread (new ParameterizedThreadStart (Number. Print ));

When to use ParameterizedThreadStart over ThreadStart delegate?

Use ParameterizedThreadStart delegate if you have some data to pass to the Thread function, otherwise just use ThreadStart delegate.

Please note: Using parameterizedThreadStart delegate and Thread. Start (Object) method to pass data to the Thread function is not type safe as they operate on object datatype and any type of data can be passed.

If you try to change the data type of the target parameter of Print () function from object to int, a compiler error will be raised as the signature of Print () function does not match with the signature of ParameterizedThreadStart delegate.

Part 90 Passing data to the Thread function in a type safe manner (in a manner... to some extent)

To pass data to the Thread function in a type safe manner, encapsulate the thread function and the data it needs in a helper class and use the ThreadStart delegate to execute the thread function.

Class Program {static void Main (string [] args) {Console. writeLine ("input a target number:"); int target = Convert. toInt32 (Console. readLine (); Number number Number = new number (target); Thread t = new Thread (Number. print); t. start (); Console. readLine () ;}} public class Number {int _ target; public Number (int target) {this. _ target = target;} public void Print () {int number = 0; for (int I = 0; I <_ target; I ++) {Console. writeLine (I );}}}View Code

Part 91 Retrieving data from Thread function using callback method

Callback method to get data from thread

Main thread retrieves (Search) the target number from the user.

Main thread creates a child thread and pass the target number to the child thread.

The child thread computes the sum of numbers and then returns the sum to the Main thread using callback function.

The callback method prints the sum of numbers.

Step 1: Create a callback delegate. The actual callback method signature shocould match with the signature of this delegate.

public delegate void SumOfNumberCallback(int sumOfNumber);

Step 2: Create a helper class to compute the sum of numbers and to call the callback method.

Public class Number {int _ target; SumOfNumberCallback _ callBackMethod; public Number (int target, SumOfNumberCallback callBackMethod) {this. _ target = target; this. _ callBackMethod = callBackMethod;} public void PrintSumOfNumber () {int sum = 0; for (int I = 1; I <= _ target; I ++) {sum = sum + I;} if (_ callBackMethod! = Null) {_ callBackMethod (sum );}}}View Code

Step 3: This class consumes the Number class create in Step 2

Class Program {public static void Main (string [] args) {Console. writeLine ("input a target number:"); int target = Convert. toInt32 (Console. readLine (); SumOfNumberCallback callBack = new SumOfNumberCallback (PrintSum); Number number Number = new number (target, callBack); Thread t = new Thread (Number. printSumOfNumber); t. start (); Console. readLine ();} public static void PrintSum (int sum) {Console. writeLine ("sum of number =" + sum );}}View Code

Related Article

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.