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 = ;
if (int.TryParse(target.ToString(), out number))
{
for (int i = ; i < number; i++)
{
Console.WriteLine(i);
}
}
}
}

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.

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 = ;
for (int i = ; i < _target; i++)
{
Console.WriteLine(i);
}
}
}

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.

public class Number
{
int _target;
SumOfNumberCallback _callBackMethod;
public Number(int target, SumOfNumberCallback callBackMethod)
{
this._target = target;
this._callBackMethod = callBackMethod;
}
public void PrintSumOfNumber()
{
int sum = ;
for (int i = ; i <= _target; i++)
{
sum = sum + i;
}
if(_callBackMethod!=null)
{
_callBackMethod(sum);
}
}
}

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 = 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);
}
}

Part 89 to 91 Talking about pass the parameters in thread的更多相关文章

  1. WPF – pass multiple parameters to a Command

    public class SendCommand : ICommand { public void Execute(object parameter) { var labels = ((object[ ...

  2. How to pass string parameters to an TADOQuery?

    http://4byte.cn/question/1130217/how-to-pass-string-parameters-to-an-tadoquery.html 从2个答案看,如果TADOQue ...

  3. How to pass multiple parameters in PowerShell invoke-restmethod

    Link: http://www.tagwith.com/question_322855_how-to-pass-parameters-in-powershell-invoke-restmethod- ...

  4. C++11 中的线程、锁和条件变量

    转自:http://blog.jobbole.com/44409/ 线程 类std::thread代表一个可执行线程,使用时必须包含头文件<thread>.std::thread可以和普通 ...

  5. AI基础架构Pass Infrastructure

    AI基础架构Pass Infrastructure Operation Pass OperationPass : Op-Specific OperationPass : Op-Agnostic Dep ...

  6. Pass Infrastructure基础架构(上)

    Pass Infrastructure基础架构(上) Operation Pass OperationPass : Op-Specific OperationPass : Op-Agnostic De ...

  7. python gettitle v2.0

    #!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...

  8. python gettitle.py

    #!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...

  9. Mask裁切UI粒子特效或者3D模型

    刚好前几天有人问我这个问题,再加上新项目也可能用,所以这两天就研究了一下.其实如果粒子特效 和3D模型 都用RenderTexture来做的话就不会有裁切的问题,但是粒子特效用RenderTextur ...

随机推荐

  1. 新唐M0 ISP下载要点

    http://blog.csdn.net/rejoice818/article/details/7736029 一.注意:官方光盘内“Software Utilities”目录下,可找到ICP或ISP ...

  2. Ajax获得站点文件内容实例

    一个简单的Ajax实例:选择一部著作,会通过 Ajax 实时获得相关的名字. 把4个html文件放到 web站点 的同一个文件下. index.html <html> <head&g ...

  3. C#如何通过NCO3.0来连接SAP并调用SAP中的RFC

    ,这是SAP针对.Net开发的专用组件,安装完成之后在C:\Program Files\SAP\SAP_DotNetConnector3_x86目录下面会有sapnco_utils.dll sapnc ...

  4. java学习笔记(3)——面向对象

    this关键字 this是当前对象的引用,是运行期间当前对象本身. 可以使用this明确的访问当前对象的属性或者方法,类似于“我” this()可以调用本类的其他构造器,可以使用构造器的重用简化代码的 ...

  5. cookie标准话

    php设置cookie setcookie( * * ,'/'); setcookie( * * ,'/'); //清除cookie setcookie("loginname",' ...

  6. android-square-progressbar-legacy

    https://github.com/eltld/android-square-progressbar-legacy

  7. SAO总结

    Application Structure Web App-------------------------------------------------------------->Serve ...

  8. 剑指 offer set 2 从头到尾打印链表

    总结 1. 书中给出的最终解法是递归或用堆栈模拟递归. 之前我一直不清楚是否还有更优雅的做法, 看样是没了

  9. 【转】winform退出代码:Application.Exit和Environment.Exit(0)

    Application.Exit和Environment.Exit(0)有什么退出方面的区别吗? Application.Exit:通知winform消息循环退出.会在所有前台线程退出后,退出应用 强 ...

  10. python实现二叉树遍历算法

    说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...