方法一:应用ParameterizedThreadStart这个委托来传递输入参数,这种方法适用于传递单个参数的情况。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. namespace BeginInvokeTest
  11. {
  12. /// <summary>
  13. /// 给线程传递参数
  14. /// </summary>
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. //第一种方法,应用ParameterizedThreadStart这个委托来传递输入参数
  24. ParameterizedThreadStart start = new ParameterizedThreadStart(ChangeText);
  25. Thread thread = new Thread(start);
  26. object obj = "HelloWorld";
  27. thread.Start(obj);
  28. }
  29. public delegate void ChangeTextDelegate(object message);
  30. public void ChangeText(object message)
  31. {
  32. //InvokeRequired是Control的一个属性(这个属性是可以在其他线程里访问的)
  33. //这个属性表明调用方是否来自非UI线程,如果是,则使用BeginInvoke来调用这个函数,否则就直接调用,省去线程封送的过程
  34. if (this.InvokeRequired)
  35. {
  36. this.BeginInvoke(new ChangeTextDelegate(ChangeText), message);
  37. }
  38. else
  39. {
  40. this.Text = message.ToString();
  41. }
  42. }
  43. }
  44. }

ParameterizedThreadStart 委托和 Thread.Start(Object) 方法重载使得将数据传递给线程过程变得简单,但由于可以将任何对象传递给 Thread.Start(Object),因此这种方法并不是类型安全的。将数据传递给线程过程的一个更可靠的方法是将线程过程和数据字段都放入辅助对 象。因此第一种方法是不推荐的。

方法二:利用线程实现类,将调用参数定义成属性的方式来操作线程参数,也就是将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。通过之种方法可以传递多个参数。

  1. using System;
  2. using System.Threading;
  3. // The ThreadWithState class contains the information needed for
  4. // a task, and the method that executes the task.
  5. //
  6. public class ThreadWithState {
  7. // State information used in the task.
  8. private string boilerplate;
  9. private int value;
  10. // The constructor obtains the state information.
  11. public ThreadWithState(string text, int number)
  12. {
  13. boilerplate = text;
  14. value = number;
  15. }
  16. // The thread procedure performs the task, such as formatting
  17. // and printing a document.
  18. public void ThreadProc()
  19. {
  20. Console.WriteLine(boilerplate, value);
  21. }
  22. }
  23. // Entry point for the example.
  24. //
  25. public class Example {
  26. public static void Main()
  27. {
  28. // Supply the state information required by the task.
  29. ThreadWithState tws = new ThreadWithState(
  30. "This report displays the number {0}.", 42);
  31. // Create a thread to execute the task, and then
  32. // start the thread.
  33. Thread t = new Thread(new ThreadStart(tws.ThreadProc));
  34. t.Start();
  35. Console.WriteLine("Main thread does some work, then waits.");
  36. t.Join();
  37. Console.WriteLine(
  38. "Independent task has completed; main thread ends.");
  39. }
  40. }

上面示例摘自MSDN

方法三:利用线程池来传递参数

方法四:利用匿名方法来传递参数,利用了匿名方法,连上面那种独立的类都省掉了,但是如果逻辑比较复杂,用这种方法就不太好了。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. namespace BeginInvokeTest
  11. {
  12. public partial class Form2 : Form
  13. {
  14. public Form2()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. Thread thread = new Thread(new ThreadStart(delegate()
  21. {
  22. this.BeginInvoke(new ChangeTextDelegate(ChangeText), "HelloWorld");
  23. }));
  24. thread.Start();
  25. }
  26. public delegate void ChangeTextDelegate(string message);
  27. public void ChangeText(string message)
  28. {
  29. this.Text = message;
  30. }
  31. }
  32. }

此外,如果需要从线程返回数据,这时可以用回调方法,下面示例摘自MSDN。

    1. using System;
    2. using System.Threading;
    3. // The ThreadWithState class contains the information needed for
    4. // a task, the method that executes the task, and a delegate
    5. // to call when the task is complete.
    6. //
    7. public class ThreadWithState {
    8. // State information used in the task.
    9. private string boilerplate;
    10. private int value;
    11. // Delegate used to execute the callback method when the
    12. // task is complete.
    13. private ExampleCallback callback;
    14. // The constructor obtains the state information and the
    15. // callback delegate.
    16. public ThreadWithState(string text, int number,
    17. ExampleCallback callbackDelegate)
    18. {
    19. boilerplate = text;
    20. value = number;
    21. callback = callbackDelegate;
    22. }
    23. // The thread procedure performs the task, such as
    24. // formatting and printing a document, and then invokes
    25. // the callback delegate with the number of lines printed.
    26. public void ThreadProc()
    27. {
    28. Console.WriteLine(boilerplate, value);
    29. if (callback != null)
    30. callback(1);
    31. }
    32. }
    33. // Delegate that defines the signature for the callback method.
    34. //
    35. public delegate void ExampleCallback(int lineCount);
    36. // Entry point for the example.
    37. //
    38. public class Example
    39. {
    40. public static void Main()
    41. {
    42. // Supply the state information required by the task.
    43. ThreadWithState tws = new ThreadWithState(
    44. "This report displays the number {0}.",
    45. 42,
    46. new ExampleCallback(ResultCallback)
    47. );
    48. Thread t = new Thread(new ThreadStart(tws.ThreadProc));
    49. t.Start();
    50. Console.WriteLine("Main thread does some work, then waits.");
    51. t.Join();
    52. Console.WriteLine(
    53. "Independent task has completed; main thread ends.");
    54. }
    55. // The callback method must match the signature of the
    56. // callback delegate.
    57. //
    58. public static void ResultCallback(int lineCount)
    59. {
    60. Console.WriteLine(
    61. "Independent task printed {0} lines.", lineCount);
    62. }
    63. }

Net线程足迹 传递参数至线程的更多相关文章

  1. C#传递参数到线程的n个方法

    [转]http://kb.cnblogs.com/a/888688/ 本片文章的议题是有关于传递参数到线程的几种方法. 首先我们要知道什么是线程,什么时候要用到线程,如何去使用线程,如何更好的利用线程 ...

  2. Jmeter 跨线程组传递参数 之两种方法

    终于搞定了Jmeter跨线程组之间传递参数,这样就不用每次发送请求B之前,都需要同时发送一下登录接口(因为同一个线程组下的请求是同时发送的),只需要发送一次登录请求,请求B直接用登录请求的参数即可,直 ...

  3. Jmeter 跨线程组传递参数 之两种方法(转)

    终于搞定了Jmeter跨线程组之间传递参数,这样就不用每次发送请求B之前,都需要同时发送一下登录接口(因为同一个线程组下的请求是同时发送的),只需要发送一次登录请求,请求B直接用登录请求的参数即可,直 ...

  4. Jmeter(五十二) - 从入门到精通高级篇 - jmeter之跨线程组传递参数(详解教程)

    1.简介 之前分享的所有文章都是只有一个线程组,而且参数的传递也只在一个线程组中,那么如果需要在两个线程组中传递参数,我们怎么做呢?宏哥今天就给小伙伴或者童鞋们讲解一下,如何实现在线程组之间传递参数. ...

  5. C++ 并发编程2 --向线程函数传递参数

    1向线程函数传递参数比较简单,一般的形式如下 void f(int i,std::string const& s);std::thread t(f,3, "hello"); ...

  6. Linux线程体传递参数的方法详解

    传递参数的两种方法 线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数. 例子 #include #include using namespace std; pthread_t thre ...

  7. C#往线程里传递参数

    Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. Thread (ThreadStart) 初始化 Th ...

  8. Jmeter跨线程组传递参数

    Jmeter的线程组之间是相互独立的,各个线程组互不影响,所以线程组A中输出的参数,是无法直接在线程组B中被调用的. 但有时候为了方便,可以把不同模块接口放在不同线程组,就涉及不同线程组传参问题,比如 ...

  9. c#线程间传递参数

    线程操作主要用到Thread类,他是定义在System.Threading.dll下.使用时需要添加这一个引用.该类提供给我们四个重载的构造函数(以下引自msdn).        Thread (P ...

随机推荐

  1. JavaScript“并非”一切皆对象

    上一篇:<函数声明和函数表达式--函数声明和函数表达式的异同> p{font-size:14px; } 写在前面 网上非常多都在说"JavaScript一切皆对象",那 ...

  2. git用法大全

    转载自实验楼,之前有更新过两篇git的文章,毕竟内容太少,而git还有很多更丰富的技能,在实验楼上有一系列全的教程,这里做一下备案.需要时查阅. Git 实战教程 目录 一.实验说明 二.git的初始 ...

  3. Jenkins架构

    一. Master 和slave. 下图阐述了master-slave交互的架构:

  4. Maven入门使用(一)

    一.什么是maven 一般认为maven是项目构建工具+依赖管理工具+项目信息管理工具. maven是一个强大的构建工具,能够帮助我们自动化构建过程. 清理.编译.测试.生成报告.打包.部署都是可以通 ...

  5. 【交叉染色法判断二分图】Claw Decomposition UVA - 11396

    题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...

  6. luogu P2107 小Z的AK计划

    最近复习了一下堆,于是去luogu上找一些简单题写一写 贪心的想,小z不会到一半以后回头去Ak,因为这样从时间上想肯定是不优的,他可以早在之间经过时就AK所以我们可以将所有机房按照横坐标排序可以想到的 ...

  7. JAVA中常见异常小结

    1.java.lang.ArithmeticException 算术运算异常,例如除数为0,所以引发了算数异常 2.Java.lang.StringIndexOutOfBoundsException: ...

  8. Unity 2D游戏开发教程之游戏中精灵的跳跃状态

    Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...

  9. hdu1232 畅通工程 并查集的 应用

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  10. 【BZOJ 4332】 4332: JSOI2012 分零食 (FFT+快速幂)

    4332: JSOI2012 分零食 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 119  Solved: 66 Description 这里是欢乐 ...