星空网 > 软件开发 > ASP.net

Part 89 to 91 Talking about pass the parameters in thread

Part 89   ParameterizedThreadStart delegate

Use ParameterizedThreadStart delegate to pass data to the thread function

Part 89 to 91  Talking about pass the parameters in threadPart 89 to 91  Talking about pass the parameters in thread
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 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.

Part 89 to 91  Talking about pass the parameters in threadPart 89 to 91  Talking about pass the parameters in thread
class Program  {    static void Main(string[] args)    {      Console.WriteLine("input a target number:");      int target = Convert.ToInt32(Console.ReadLine());      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(检索) 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 should 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.

Part 89 to 91  Talking about pass the parameters in threadPart 89 to 91  Talking about pass the parameters in thread
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

Part 89 to 91  Talking about pass the parameters in threadPart 89 to 91  Talking about pass the parameters in thread
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 = 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



原标题:Part 89 to 91 Talking about pass the parameters in thread

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

TikTok转化极低!DTC却很火爆!:https://www.kjdsnews.com/a/553663.html
【亚马逊】大卖小卖是如何测款的:https://www.kjdsnews.com/a/553664.html
深圳大卖第二轮裁员!30岁员工坐门口嚎啕大哭……:https://www.kjdsnews.com/a/553665.html
疫情期间,东南亚电商发展的三大趋势:https://www.kjdsnews.com/a/553666.html
亚马逊封号潮下,卖家们凭借这么做实现自救?速卖通和Lazada成为首选:https://www.kjdsnews.com/a/553667.html
做Shopify需要VAT吗?附VAT注册方法:https://www.kjdsnews.com/a/553668.html
大批Listing被下架,“黄色警告”!提示存在停用风险:https://www.kjdsnews.com/a/1836647.html
跨境支付百科——巴西支付篇:https://www.kjdsnews.com/a/1836648.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流