方法一:应用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. }

最新文章

  1. Ubuntu学习总结-09 安装 Pycharm
  2. SQL Server 权限管理
  3. gulp-rev-collector自定义修改rev-manifest.json后替换不成功的问题分析
  4. 【JAVA】Runtime
  5. Git.Framework 框架随手记--存储过程简化
  6. IOS 设备参数
  7. linux进程调度方法(SCHED_OTHER,SCHED_FIFO,SCHED_RR)
  8. C#中的Delegate
  9. 在linux/unix中查找大文件
  10. XML新增、修改、选择
  11. c#文件转化byte数组
  12. STL algorithm算法merge(34)
  13. 什么是DOM
  14. Php面向对象 – 单例模式
  15. 使用dom4j生成xml字符串,以及解析xml字符串
  16. CXF.bat
  17. Ricequant-米筐金工-估值因子
  18. apue——读目录操作
  19. C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题
  20. Python 连接MongoDB并比较两个字符串相似度的简单示例

热门文章

  1. 使用OpenSSL自签发服务器https证书
  2. thinkphp5.1使用phpstudy隐藏index.php
  3. python collection系列
  4. java EE :Servlet 接口
  5. loadrunner中大小写字符转换函数封装:
  6. 十五oracle 触发器
  7. 2017-2018-1 20179202《Linux内核原理与分析》第五周作业
  8. 【记录】mysql 5.7.20安装 出现...mysql-5.7.20-winx64\data\is_writable’ Errcode: 2 - No such file or directory
  9. openstack多region配置
  10. redis_NoSql入门概述数据模型简介