博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用ParameterizedThreadStart委托向线程函数传送参数
阅读量:4116 次
发布时间:2019-05-25

本文共 3857 字,大约阅读时间需要 12 分钟。

在不传递参数情况下,一般大家都使用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.   
  6. namespace ThreadAbort  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             System.Console.WriteLine("主线程开始");  
  13.             //创建线程对象  
  14.             MyThread obj = new MyThread();  
  15.             Thread th = new Thread(new ParameterizedThreadStart(obj.SomeLongTask));  
  16.             th.IsBackground = true;  
  17.             th.Start(10);//启动线程,传递参数10  
  18.             th.Join();  
  19.             System.Console.WriteLine("主线程结束");  
  20.         }  
  21.     }  
  22.   
  23.     class MyThread  
  24.     {  
  25.         public void SomeLongTask(object obj)  
  26.         {  
  27.             int n = Convert.ToInt32(obj); //将接收的参数转换为需要的类型  
  28.             System.Console.WriteLine("辅助线程开始...");  
  29.             for (int i = 0; i <= n; i++)  
  30.             {  
  31.                 System.Console.WriteLine(i);  
  32.                 Thread.Sleep(100);  
  33.             }  
  34.         }  
  35.     }  
  36. }  

二.多参数、有返回值

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

    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.   
  6. namespace ThreadTest  
  7. {  
  8.     class ThreadMethodHelper  
  9.     {  
  10.         //线程输入参数  
  11.         public int x;  
  12.         public int y;  
  13.         //函数返回值  
  14.         public long returnVaule;  
  15.     }  
  16.     class MultiParas  
  17.     {  
  18.         public static void SomeTask(object argu)  
  19.         {  
  20.             long ret = 0;  
  21.             int x = (argu as ThreadMethodHelper).x;  
  22.             int y = (argu as ThreadMethodHelper).y;  
  23.             //使用x和y完成一些工作,结果保存在ret中  
  24.             ret = x * y;  
  25.             (argu as ThreadMethodHelper).returnVaule= ret;  
  26.         }  
  27.         static void Main(string[] args)  
  28.         {  
  29.             System.Console.WriteLine("主线程开始");  
  30.             ThreadMethodHelper arg = new ThreadMethodHelper{x = 10, y = 100};  
  31.             //创建线程对象  
  32.             Thread th = new Thread(new ParameterizedThreadStart(SomeTask));  
  33.             //Thread th = new Thread(SomeTask);//这样写也可以  
  34.             th.IsBackground = true;  
  35.             th.Start(arg);//启动线程,传递参数10  
  36.             th.Join();  
  37.             Console.WriteLine("the result is :" + arg.returnVaule);  
  38.             System.Console.WriteLine("主线程结束");  
  39.         }  
  40.     }  
  41. }  
例2:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Threading;  
  5.   
  6. namespace UseArray  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Thread th = new Thread(DoWithArray);  
  13.             ThreadMethodHelper argu = new ThreadMethodHelper();  
  14.             argu.arr = new int[] { -1, 9, 100, 78, 23, 54, -90 };  
  15.             th.Start(argu);  
  16.             th.Join();  
  17.             Console.WriteLine("数组元素清单");  
  18.             foreach (int i in argu.arr)  
  19.             {  
  20.                 Console.Write(i.ToString() + "  ");  
  21.             }  
  22.             Console.WriteLine();  
  23.             Console.WriteLine("最大值:{0}", argu.MaxValue);  
  24.             Console.WriteLine("最小值:{0}", argu.MinValue);  
  25.             Console.WriteLine("总和:{0}", argu.Sum );  
  26.             Console.WriteLine("平均值:{0}", argu.Average );  
  27.   
  28.             Console.ReadKey();  
  29.         }  
  30.   
  31.         static void DoWithArray(object  obj)  
  32.         {  
  33.             ThreadMethodHelper argu = obj as ThreadMethodHelper;  
  34.             for (int i = 0; i < argu.arr.Length; i++)  
  35.             {  
  36.                 if (argu.arr[i] > argu.MaxValue)  
  37.                     argu.MaxValue = argu.arr[i];  
  38.                 if (argu.arr[i] < argu.MinValue)  
  39.                     argu.MinValue = argu.arr[i];  
  40.                 argu.Sum += argu.arr[i];  
  41.             }  
  42.             argu.Average = argu.Sum / argu.arr.Length;  
  43.         }  
  44.     }  
  45.   
  46.     //封装线程的输入和输出信息  
  47.     class ThreadMethodHelper  
  48.     {  
  49.         //线程输入参数  
  50.         public int[] arr;  
  51.         //函数返回值  
  52.         public int MaxValue=0;  
  53.         public int MinValue=0;  
  54.         public long Sum=0;  
  55.         public double Average=0;  
  56.     }  
  57. }  

转载地址:http://cnupi.baihongyu.com/

你可能感兴趣的文章
日志框架logj的使用
查看>>
js-高德地图规划路线
查看>>
常用js收集
查看>>
mydata97的日期控件
查看>>
如何防止sql注入
查看>>
maven多工程构建与打包
查看>>
springmvc传值
查看>>
Java 集合学习一 HashSet
查看>>
在Eclipse中查看Android源码
查看>>
Android-Socket登录实例
查看>>
Android使用webservice客户端实例
查看>>
层在页面中的定位
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>