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. MON166 FAQ

    MON166: SOFTWARE RESET USING THE MONITOR QUESTION What happens when debugging using MON166 and my pr ...

  2. css margin的相关属性,问题及应用

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=48 引言:margin ...

  3. java最简单的方式实现httpget和httppost请求

    java实现httpget和httppost请求的方式多种多样,个人总结了一种最简单的方式,仅仅需几行代码,就能够完美的实现. 此处须要用到两个jar包,httpclient-4.3.1.jar.ht ...

  4. Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8

    在使用Eclipse 直接编译NDK,有时候会报类似以下错误 Android NDK: WARNING: APP_PLATFORM android-14 is larger than android: ...

  5. Java项目经验——程序员成长的关键(转载)

    Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点:1.掌握项目开发的基本步骤2.具备极强的面向对象的分析与设计技巧3.掌握用例驱动.以 ...

  6. socket通信简介

    转:http://blog.csdn.net/xiaoweige207/article/details/6211577 “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的 ...

  7. cplusplus系列>algorithm>std::for_each

    http://www.cplusplus.com/reference/algorithm/for_each/ 对一个序列应用函数.可以是函数指针,或者是functor. // for_each exa ...

  8. Android进阶笔记19:onInterceptTouchEvent、onTouchEvent与onTouch

    1.onTouch方法:onTouch方法是View的 OnTouchListener借口中定义的方法,处理View及其子类被touch是的事件处理.当一个View绑定了OnTouchLister后, ...

  9. 用英文加优先级来解读C的声明

    比如:int ( * func_p ) ( double ); 首先着眼于标识符. func_p is 因为存在括号,(* func_p) 先被处理,这里着眼于* func_p is a pointe ...

  10. 《算法导论》习题解答 Chapter 22.1-7(关联矩阵的性质)

    主对角线:出度+入度 其他:arr[i][j]=-n,则i与j之间有n条边. 证明: (原文点此,索引目录.感谢xiazdong君 && Google酱.这里是偶尔做做搬运工的水果君( ...