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
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的更多相关文章
- WPF – pass multiple parameters to a Command
public class SendCommand : ICommand { public void Execute(object parameter) { var labels = ((object[ ...
- How to pass string parameters to an TADOQuery?
http://4byte.cn/question/1130217/how-to-pass-string-parameters-to-an-tadoquery.html 从2个答案看,如果TADOQue ...
- How to pass multiple parameters in PowerShell invoke-restmethod
Link: http://www.tagwith.com/question_322855_how-to-pass-parameters-in-powershell-invoke-restmethod- ...
- C++11 中的线程、锁和条件变量
转自:http://blog.jobbole.com/44409/ 线程 类std::thread代表一个可执行线程,使用时必须包含头文件<thread>.std::thread可以和普通 ...
- AI基础架构Pass Infrastructure
AI基础架构Pass Infrastructure Operation Pass OperationPass : Op-Specific OperationPass : Op-Agnostic Dep ...
- Pass Infrastructure基础架构(上)
Pass Infrastructure基础架构(上) Operation Pass OperationPass : Op-Specific OperationPass : Op-Agnostic De ...
- python gettitle v2.0
#!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...
- python gettitle.py
#!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...
- Mask裁切UI粒子特效或者3D模型
刚好前几天有人问我这个问题,再加上新项目也可能用,所以这两天就研究了一下.其实如果粒子特效 和3D模型 都用RenderTexture来做的话就不会有裁切的问题,但是粒子特效用RenderTextur ...
随机推荐
- Qt技巧:QProcess与外部程序的调用
项目做到一定阶段,常常须要在原来的project上调用外部程序. Qt为此提供了QProcess类,QProcess可用于完毕启动外部程序,并与之交互通信. 一.启动外部程序的两种方式: (1)一体式 ...
- Redis for Windows(C#缓存)安装和使用
Redis for Windows(C#缓存)安装和使用 前言 前段时间写过两篇简单的有关Memcached缓存的相关文章,当然了只是入门的如何使用而已.最近这两天又发现了Redis这个神奇的东西,之 ...
- ssl/https双向验证的配置
1.SSL认证 不需要特别配置,相关证书库生成看https认证中的相关部分 2.HTTPS认证 一.基本概念 1.单向认证,就是传输的数据加密过了,但是不会校验客户端的来源 2.双向认证,如果客户端 ...
- cocos2d-x 2.2 移植wp8遇到的坑
这两天正在将之前的一款cocos2d游戏,移植到wp平台上,这里记录一下所遇到的问题以及解决方法. 我是用的cocos2d下面的例子程序进行修改的. 遇到的第一个问题是资源路径的问题,当时我把解决方案 ...
- WCF摘记
//绑定形式 NetTcpBinding bind = new NetTcpBinding(); //地址 EndpointAddress address = new EndpointAddress( ...
- 堆排序-C语言实现
堆排序 堆排序是利用堆的性质进行的一种选择排序.下面先讨论一下堆. 1.堆 堆实际上是一棵完全二叉树,其任何一非叶节点满足性质: Key[i]<=key[2i+1]&&Key[i ...
- 关于OpenGL+GLSL深度贴图采样
作者:Nin+.Lee 邮箱:lilei9110@gmail.com * 本文属原创,转载请注明出处. 在GLSL中,存在着sampler2D和sampler2DShadow两种2D贴图采样器.在对一 ...
- 进程产生的三种方式:fork、system和exec
1.fork()方式 fork()函数以父进程为蓝本复制一个进程,其ID号与父进程ID号不同.在Linux环境下,fork()是以写复制实现的,只有内存等与父进程不同,其他与父进程共享,只有在父进程或 ...
- 数据连接命令join
join主要用来将两个相关联的文件连接起来.两个文件相关联的意思是指这两个文件中有一些字段是关联的,例如两个文件的第1个字段都是学号,且每个学生的学号是唯一的.像这种具有唯一性关联的文件,就可以使用j ...
- IDEA加密(转)
1. 简介 IDEA是International Data Encryption Algorithm 的缩写,是1990年由瑞士联邦技术学院来学嘉X.J.Lai 和Massey提出的建议标准算法称作P ...