Net线程足迹 传递参数至线程
方法一:应用ParameterizedThreadStart这个委托来传递输入参数,这种方法适用于传递单个参数的情况。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace BeginInvokeTest
- {
- /// <summary>
- /// 给线程传递参数
- /// </summary>
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //第一种方法,应用ParameterizedThreadStart这个委托来传递输入参数
- ParameterizedThreadStart start = new ParameterizedThreadStart(ChangeText);
- Thread thread = new Thread(start);
- object obj = "HelloWorld";
- thread.Start(obj);
- }
- public delegate void ChangeTextDelegate(object message);
- public void ChangeText(object message)
- {
- //InvokeRequired是Control的一个属性(这个属性是可以在其他线程里访问的)
- //这个属性表明调用方是否来自非UI线程,如果是,则使用BeginInvoke来调用这个函数,否则就直接调用,省去线程封送的过程
- if (this.InvokeRequired)
- {
- this.BeginInvoke(new ChangeTextDelegate(ChangeText), message);
- }
- else
- {
- this.Text = message.ToString();
- }
- }
- }
- }
ParameterizedThreadStart 委托和 Thread.Start(Object) 方法重载使得将数据传递给线程过程变得简单,但由于可以将任何对象传递给 Thread.Start(Object),因此这种方法并不是类型安全的。将数据传递给线程过程的一个更可靠的方法是将线程过程和数据字段都放入辅助对 象。因此第一种方法是不推荐的。
方法二:利用线程实现类,将调用参数定义成属性的方式来操作线程参数,也就是将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。通过之种方法可以传递多个参数。
- using System;
- using System.Threading;
- // The ThreadWithState class contains the information needed for
- // a task, and the method that executes the task.
- //
- public class ThreadWithState {
- // State information used in the task.
- private string boilerplate;
- private int value;
- // The constructor obtains the state information.
- public ThreadWithState(string text, int number)
- {
- boilerplate = text;
- value = number;
- }
- // The thread procedure performs the task, such as formatting
- // and printing a document.
- public void ThreadProc()
- {
- Console.WriteLine(boilerplate, value);
- }
- }
- // Entry point for the example.
- //
- public class Example {
- public static void Main()
- {
- // Supply the state information required by the task.
- ThreadWithState tws = new ThreadWithState(
- "This report displays the number {0}.", 42);
- // Create a thread to execute the task, and then
- // start the thread.
- Thread t = new Thread(new ThreadStart(tws.ThreadProc));
- t.Start();
- Console.WriteLine("Main thread does some work, then waits.");
- t.Join();
- Console.WriteLine(
- "Independent task has completed; main thread ends.");
- }
- }
上面示例摘自MSDN
方法三:利用线程池来传递参数
方法四:利用匿名方法来传递参数,利用了匿名方法,连上面那种独立的类都省掉了,但是如果逻辑比较复杂,用这种方法就不太好了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace BeginInvokeTest
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Thread thread = new Thread(new ThreadStart(delegate()
- {
- this.BeginInvoke(new ChangeTextDelegate(ChangeText), "HelloWorld");
- }));
- thread.Start();
- }
- public delegate void ChangeTextDelegate(string message);
- public void ChangeText(string message)
- {
- this.Text = message;
- }
- }
- }
此外,如果需要从线程返回数据,这时可以用回调方法,下面示例摘自MSDN。
- using System;
- using System.Threading;
- // The ThreadWithState class contains the information needed for
- // a task, the method that executes the task, and a delegate
- // to call when the task is complete.
- //
- public class ThreadWithState {
- // State information used in the task.
- private string boilerplate;
- private int value;
- // Delegate used to execute the callback method when the
- // task is complete.
- private ExampleCallback callback;
- // The constructor obtains the state information and the
- // callback delegate.
- public ThreadWithState(string text, int number,
- ExampleCallback callbackDelegate)
- {
- boilerplate = text;
- value = number;
- callback = callbackDelegate;
- }
- // The thread procedure performs the task, such as
- // formatting and printing a document, and then invokes
- // the callback delegate with the number of lines printed.
- public void ThreadProc()
- {
- Console.WriteLine(boilerplate, value);
- if (callback != null)
- callback(1);
- }
- }
- // Delegate that defines the signature for the callback method.
- //
- public delegate void ExampleCallback(int lineCount);
- // Entry point for the example.
- //
- public class Example
- {
- public static void Main()
- {
- // Supply the state information required by the task.
- ThreadWithState tws = new ThreadWithState(
- "This report displays the number {0}.",
- 42,
- new ExampleCallback(ResultCallback)
- );
- Thread t = new Thread(new ThreadStart(tws.ThreadProc));
- t.Start();
- Console.WriteLine("Main thread does some work, then waits.");
- t.Join();
- Console.WriteLine(
- "Independent task has completed; main thread ends.");
- }
- // The callback method must match the signature of the
- // callback delegate.
- //
- public static void ResultCallback(int lineCount)
- {
- Console.WriteLine(
- "Independent task printed {0} lines.", lineCount);
- }
- }
Net线程足迹 传递参数至线程的更多相关文章
- C#传递参数到线程的n个方法
[转]http://kb.cnblogs.com/a/888688/ 本片文章的议题是有关于传递参数到线程的几种方法. 首先我们要知道什么是线程,什么时候要用到线程,如何去使用线程,如何更好的利用线程 ...
- Jmeter 跨线程组传递参数 之两种方法
终于搞定了Jmeter跨线程组之间传递参数,这样就不用每次发送请求B之前,都需要同时发送一下登录接口(因为同一个线程组下的请求是同时发送的),只需要发送一次登录请求,请求B直接用登录请求的参数即可,直 ...
- Jmeter 跨线程组传递参数 之两种方法(转)
终于搞定了Jmeter跨线程组之间传递参数,这样就不用每次发送请求B之前,都需要同时发送一下登录接口(因为同一个线程组下的请求是同时发送的),只需要发送一次登录请求,请求B直接用登录请求的参数即可,直 ...
- Jmeter(五十二) - 从入门到精通高级篇 - jmeter之跨线程组传递参数(详解教程)
1.简介 之前分享的所有文章都是只有一个线程组,而且参数的传递也只在一个线程组中,那么如果需要在两个线程组中传递参数,我们怎么做呢?宏哥今天就给小伙伴或者童鞋们讲解一下,如何实现在线程组之间传递参数. ...
- C++ 并发编程2 --向线程函数传递参数
1向线程函数传递参数比较简单,一般的形式如下 void f(int i,std::string const& s);std::thread t(f,3, "hello"); ...
- Linux线程体传递参数的方法详解
传递参数的两种方法 线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数. 例子 #include #include using namespace std; pthread_t thre ...
- C#往线程里传递参数
Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. Thread (ThreadStart) 初始化 Th ...
- Jmeter跨线程组传递参数
Jmeter的线程组之间是相互独立的,各个线程组互不影响,所以线程组A中输出的参数,是无法直接在线程组B中被调用的. 但有时候为了方便,可以把不同模块接口放在不同线程组,就涉及不同线程组传参问题,比如 ...
- c#线程间传递参数
线程操作主要用到Thread类,他是定义在System.Threading.dll下.使用时需要添加这一个引用.该类提供给我们四个重载的构造函数(以下引自msdn). Thread (P ...
随机推荐
- 深度学习方法(十二):卷积神经网络结构变化——Spatial Transformer Networks
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.机器学习技术感兴趣的同学加入. 今天具体介绍一个Google ...
- HTTP协议头注射漏洞实例
HTTP 响应头文件中包含未经验证的数据会引发 cache-poisoning.cross-site scripting.cross-user defacement.page hijacking.co ...
- 认识hasLayout——IE浏览器css bug的一大罪恶根源 (转)
认识hasLayout--IE浏览器css bug的一大罪恶根源 转 什么是hasLayout?hasLayout是IE特有的一个属性.很多的ie下的css bug都与其息息相关.在ie中,一个元素要 ...
- linux 下vim文件乱码 cat文件正常处理方法
linux 下vim文件乱码 cat文件正常处理方法 服务器支持中文字符集,cat和其他查看文件命令现在正常,vim还是出现了中文乱码问题, 1.查看文件编码格式 vim 文件 :set fileen ...
- linux 把ls -R格式化成树状结构
谁能写脚本把linux中的ls -R命令的结果格式化成树状结构? 最好是shell脚本!欢迎讨论! 参与讨论有可能意外获取iPhone6哦~~
- C++的一道变态题
题目大概是这样的:有两个数组a[N],b[N],求构造 b[i]=a[0]*a[1]*a[2]*...a[N-1]/a[i], 要求: .不能使用除法. .空间复杂度O(1),时间复杂度O(n). . ...
- BNUOJ 52517 Another Server
网络流. 似乎有别的做法,没想. #include<bits/stdc++.h> using namespace std; + ; const int INF = 0x7FFFFFFF; ...
- python每天定时发送短信脚本
最近业务上需要每天解析txt文本或者excel文件,读取内容发送短信,发送的时间段可控,用python实现 安装pip依赖 pip install -r requirement.txt xlrd Py ...
- 在树莓派3B上安装node.js
本文主讲如何在树莓派3B上安装node.js 环境描述1. 树莓派安装了`2016-11-25-raspbian-jessie-lite`(PS:在此版本的镜像中,默认禁用了ssh,在烧录好镜像之后, ...
- Linux通过FTP上传文件到服务器
1.如果没有安装ftp,可执行: 输入:yum -y install ftp,回车 等待安装完毕 2.连接服务器 输入:ftp 服务器IP,回车 根据提示输入用户名和密码 3.上传下载操作 1). 上 ...