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 ...
随机推荐
- Entity Framework 6 Code First +MVC5+MySql/Oracle使用过程中的几个问题
1. namespace Snapsia.Web.Models { using System; using System.Data.Entity; using System.ComponentMode ...
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...
- C# GDI在控件上绘图
本文以以在chart控件上和窗体上画矩形为例子 不多解释了,代码很简单. 还有一些童鞋要别的源码,给我发邮箱吧 using System; using System.Collections.Gener ...
- nicehair
https://github.com/lanyj189/nicehair https://github.com/eltld/nicehair https://github.com/pepoc/Cust ...
- android标题栏(titlebar)显示进度条
在后台线程中执行各种操作(网络连接.大数据存储)的时候,我们希望让客户能看到后台有操作在进行,那么既能有效的提示用户,又不占用当前操作空间,最好的方法就是在标题栏有个进度条. [代码] [Java]代 ...
- 项目源码--Android视频MV类网站客户端
下载源码 技术要点: 1.视频MV类网站客户端框架 2.底部TAB功能模块 3.用户管理模块 4.结合优质动画技术,良好的用户体验 5.用户设置模块 6.sqlite数据库灵活的应用 7.源码带有非常 ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- nie题目-游戏排行榜设计
一个mmorpg游戏,玩家众多,需要对玩家战斗力进行排行,并且战斗力变化时需要及时刷新.需要设计一个这样的排行榜. 关于海量数据排行榜的做法,云风在他的博客里给过思路,谈谈陌陌争霸在数据库方面踩过的坑 ...
- JavaFX前言
笔者在一家互联网公司做JavaEE开发,公司开发了移动端的产品,唯独没有PC端的产品,于是领导将任务分配给笔者. 使用Java开发PC客户端,我的第一反应是使用swing API.但是,产品的需求是客 ...
- 2.1.5 用SSS扫描器实施扫描
SSS(Shadow Security Scaner)是一款著名的系统漏洞扫描器,可对很大范围内的系统漏洞进行安全.高效.可靠的安全检测,其系统扫描的速度与精度足以让用户敢和专业安全机构的人以及那些专 ...