前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用ParameterizedThreadStart委托向线程函数传送参数「建议收藏」

使用ParameterizedThreadStart委托向线程函数传送参数「建议收藏」

作者头像
全栈程序员站长
发布2022-09-14 19:17:00
4340
发布2022-09-14 19:17:00
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。在不传递参数情况下,一般大家都使用ThreadStart代理来连接执行函数,ThreadStart委托接收的函数不能有参数,也不能有返回值。如果希望传递参数给执行函数,则可以使用带参数的ParameterizedThreadStart委托,

public delegate void ParameterizedThreadStart(Object obj)

可以将要传送给线程函数的信息封装为一个对象,然后调用Thread类的以下构造函数

public Thread (ParameterizedThreadStartstart)

启动线程时,向其传送一个参数信息

Thread t = new Thread(new ParameterizedThreadStart(线程函数)); t.Start(object nParam);

其中object nParam就是要传递的参数,之所以使用object类型,那是因为nParam可以是任何class类型,这样你就可传递任何类型给执行函数.

根据参数个数和返回值的不同又分为以下几种情形:

一.单参数、无返回值

这是最简单最直接的情形,无需做其他处理,直接传递

[csharp]

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace ThreadAbort
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. System.Console.WriteLine(“主线程开始”);
  12. //创建线程对象
  13. MyThread obj = new MyThread();
  14. Thread th = new Thread(new ParameterizedThreadStart(obj.SomeLongTask));
  15. th.IsBackground = true;
  16. th.Start(10);//启动线程,传递参数10
  17. th.Join();
  18. System.Console.WriteLine(“主线程结束”);
  19. }
  20. }
  21. class MyThread
  22. {
  23. public void SomeLongTask(object obj)
  24. {
  25. int n = Convert.ToInt32(obj); //将接收的参数转换为需要的类型
  26. System.Console.WriteLine(“辅助线程开始…”);
  27. for (int i = 0; i <= n; i++)
  28. {
  29. System.Console.WriteLine(i);
  30. Thread.Sleep(100);
  31. }
  32. }
  33. }
  34. }

二.多参数、有返回值

需要创建一个参数辅助类用于传递参数和返回值,例如:

class ThreadMethodHelper { //线程输入参数 public intx; public inty; //函数返回值 public long returnVaule; }

然后改造线程函数为ParameterizedThreadStart委托支持的形式

public void SomeFunc(object argu) { long ret = 0; intx = (arguas ThreadMethodHelper).x; inty = (arguas ThreadMethodHelper).y; //使用x和y完成一些工作,结果保存在ret中 (arguas ThreadMethodHelper).returnVaule= ret; }

最后就可以使用辅助类进行线程操作了

MyThreadobj= new MyThread(); varargu= new ThreadMethodHelper();

//设定线程函数参数 argu.x= 100; argu.y= 200;

//创建线程对象 Thread t = new Thread(new ParameterizedThreadStart(obj.SomeFunc));

//启动线程,向线程传送线程参数 t.Start(argu);

//主线程干其他事…… t.Join();//等待辅助线程结束

Console.WriteLine(argu.returnVaule); //取回线程结果

例1:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace ThreadTest
  6. {
  7. class ThreadMethodHelper
  8. {
  9. //线程输入参数
  10. public int x;
  11. public int y;
  12. //函数返回值
  13. public long returnVaule;
  14. }
  15. class MultiParas
  16. {
  17. public static void SomeTask(object argu)
  18. {
  19. long ret = 0;
  20. int x = (argu as ThreadMethodHelper).x;
  21. int y = (argu as ThreadMethodHelper).y;
  22. //使用x和y完成一些工作,结果保存在ret中
  23. ret = x * y;
  24. (argu as ThreadMethodHelper).returnVaule= ret;
  25. }
  26. static void Main(string[] args)
  27. {
  28. System.Console.WriteLine(“主线程开始”);
  29. ThreadMethodHelper arg = new ThreadMethodHelper{x = 10, y = 100};
  30. //创建线程对象
  31. Thread th = new Thread(new ParameterizedThreadStart(SomeTask));
  32. //Thread th = new Thread(SomeTask);//这样写也可以
  33. th.IsBackground = true;
  34. th.Start(arg);//启动线程,传递参数10
  35. th.Join();
  36. Console.WriteLine(“the result is :” + arg.returnVaule);
  37. System.Console.WriteLine(“主线程结束”);
  38. }
  39. }
  40. }

例2:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace UseArray
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Thread th = new Thread(DoWithArray);
  12. ThreadMethodHelper argu = new ThreadMethodHelper();
  13. argu.arr = new int[] { -1, 9, 100, 78, 23, 54, -90 };
  14. th.Start(argu);
  15. th.Join();
  16. Console.WriteLine(“数组元素清单”);
  17. foreach (int i in argu.arr)
  18. {
  19. Console.Write(i.ToString() + ” “);
  20. }
  21. Console.WriteLine();
  22. Console.WriteLine(“最大值:{0}”, argu.MaxValue);
  23. Console.WriteLine(“最小值:{0}”, argu.MinValue);
  24. Console.WriteLine(“总和:{0}”, argu.Sum );
  25. Console.WriteLine(“平均值:{0}”, argu.Average );
  26. Console.ReadKey();
  27. }
  28. static void DoWithArray(object obj)
  29. {
  30. ThreadMethodHelper argu = obj as ThreadMethodHelper;
  31. for (int i = 0; i < argu.arr.Length; i++)
  32. {
  33. if (argu.arr[i] > argu.MaxValue)
  34. argu.MaxValue = argu.arr[i];
  35. if (argu.arr[i] < argu.MinValue)
  36. argu.MinValue = argu.arr[i];
  37. argu.Sum += argu.arr[i];
  38. }
  39. argu.Average = argu.Sum / argu.arr.Length;
  40. }
  41. }
  42. //封装线程的输入和输出信息
  43. class ThreadMethodHelper
  44. {
  45. //线程输入参数
  46. public int[] arr;
  47. //函数返回值
  48. public int MaxValue=0;
  49. public int MinValue=0;
  50. public long Sum=0;
  51. public double Average=0;
  52. }
  53. }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159637.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.单参数、无返回值
  • 二.多参数、有返回值
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档