使用ParameterizedThreadStart委托向线程函数传送参数
2012-09-10 09:33
549 查看
在不传递参数情况下,一般大家都使用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]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadAbort
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("主线程开始");
//创建线程对象
MyThread obj = new MyThread();
Thread th = new Thread(new ParameterizedThreadStart(obj.SomeLongTask));
th.IsBackground = true;
th.Start(10);//启动线程,传递参数10
th.Join();
System.Console.WriteLine("主线程结束");
}
}
class MyThread
{
public void SomeLongTask(object obj)
{
int n = Convert.ToInt32(obj); //将接收的参数转换为需要的类型
System.Console.WriteLine("辅助线程开始...");
for (int i = 0; i <= n; i++)
{
System.Console.WriteLine(i);
Thread.Sleep(100);
}
}
}
}
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:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadTest
{
class ThreadMethodHelper
{
//线程输入参数
public int x;
public int y;
//函数返回值
public long returnVaule;
}
class MultiParas
{
public static void SomeTask(object argu)
{
long ret = 0;
int x = (argu as ThreadMethodHelper).x;
int y = (argu as ThreadMethodHelper).y;
//使用x和y完成一些工作,结果保存在ret中
ret = x * y;
(argu as ThreadMethodHelper).returnVaule= ret;
}
static void Main(string[] args)
{
System.Console.WriteLine("主线程开始");
ThreadMethodHelper arg = new ThreadMethodHelper{x = 10, y = 100};
//创建线程对象
Thread th = new Thread(new ParameterizedThreadStart(SomeTask));
//Thread th = new Thread(SomeTask);//这样写也可以
th.IsBackground = true;
th.Start(arg);//启动线程,传递参数10
th.Join();
Console.WriteLine("the result is :" + arg.returnVaule);
System.Console.WriteLine("主线程结束");
}
}
}
例2:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace UseArray
{
class Program
{
static void Main(string[] args)
{
Thread th = new Thread(DoWithArray);
ThreadMethodHelper argu = new ThreadMethodHelper();
argu.arr = new int[] { -1, 9, 100, 78, 23, 54, -90 };
th.Start(argu);
th.Join();
Console.WriteLine("数组元素清单");
foreach (int i in argu.arr)
{
Console.Write(i.ToString() + " ");
}
Console.WriteLine();
Console.WriteLine("最大值:{0}", argu.MaxValue);
Console.WriteLine("最小值:{0}", argu.MinValue);
Console.WriteLine("总和:{0}", argu.Sum );
Console.WriteLine("平均值:{0}", argu.Average );
Console.ReadKey();
}
static void DoWithArray(object obj)
{
ThreadMethodHelper argu = obj as ThreadMethodHelper;
for (int i = 0; i < argu.arr.Length; i++)
{
if (argu.arr[i] > argu.MaxValue)
argu.MaxValue = argu.arr[i];
if (argu.arr[i] < argu.MinValue)
argu.MinValue = argu.arr[i];
argu.Sum += argu.arr[i];
}
argu.Average = argu.Sum / argu.arr.Length;
}
}
//封装线程的输入和输出信息
class ThreadMethodHelper
{
//线程输入参数
public int[] arr;
//函数返回值
public int MaxValue=0;
public int MinValue=0;
public long Sum=0;
public double Average=0;
}
}
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]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadAbort
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("主线程开始");
//创建线程对象
MyThread obj = new MyThread();
Thread th = new Thread(new ParameterizedThreadStart(obj.SomeLongTask));
th.IsBackground = true;
th.Start(10);//启动线程,传递参数10
th.Join();
System.Console.WriteLine("主线程结束");
}
}
class MyThread
{
public void SomeLongTask(object obj)
{
int n = Convert.ToInt32(obj); //将接收的参数转换为需要的类型
System.Console.WriteLine("辅助线程开始...");
for (int i = 0; i <= n; i++)
{
System.Console.WriteLine(i);
Thread.Sleep(100);
}
}
}
}
二.多参数、有返回值
需要创建一个参数辅助类用于传递参数和返回值,例如: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:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadTest
{
class ThreadMethodHelper
{
//线程输入参数
public int x;
public int y;
//函数返回值
public long returnVaule;
}
class MultiParas
{
public static void SomeTask(object argu)
{
long ret = 0;
int x = (argu as ThreadMethodHelper).x;
int y = (argu as ThreadMethodHelper).y;
//使用x和y完成一些工作,结果保存在ret中
ret = x * y;
(argu as ThreadMethodHelper).returnVaule= ret;
}
static void Main(string[] args)
{
System.Console.WriteLine("主线程开始");
ThreadMethodHelper arg = new ThreadMethodHelper{x = 10, y = 100};
//创建线程对象
Thread th = new Thread(new ParameterizedThreadStart(SomeTask));
//Thread th = new Thread(SomeTask);//这样写也可以
th.IsBackground = true;
th.Start(arg);//启动线程,传递参数10
th.Join();
Console.WriteLine("the result is :" + arg.returnVaule);
System.Console.WriteLine("主线程结束");
}
}
}
例2:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace UseArray
{
class Program
{
static void Main(string[] args)
{
Thread th = new Thread(DoWithArray);
ThreadMethodHelper argu = new ThreadMethodHelper();
argu.arr = new int[] { -1, 9, 100, 78, 23, 54, -90 };
th.Start(argu);
th.Join();
Console.WriteLine("数组元素清单");
foreach (int i in argu.arr)
{
Console.Write(i.ToString() + " ");
}
Console.WriteLine();
Console.WriteLine("最大值:{0}", argu.MaxValue);
Console.WriteLine("最小值:{0}", argu.MinValue);
Console.WriteLine("总和:{0}", argu.Sum );
Console.WriteLine("平均值:{0}", argu.Average );
Console.ReadKey();
}
static void DoWithArray(object obj)
{
ThreadMethodHelper argu = obj as ThreadMethodHelper;
for (int i = 0; i < argu.arr.Length; i++)
{
if (argu.arr[i] > argu.MaxValue)
argu.MaxValue = argu.arr[i];
if (argu.arr[i] < argu.MinValue)
argu.MinValue = argu.arr[i];
argu.Sum += argu.arr[i];
}
argu.Average = argu.Sum / argu.arr.Length;
}
}
//封装线程的输入和输出信息
class ThreadMethodHelper
{
//线程输入参数
public int[] arr;
//函数返回值
public int MaxValue=0;
public int MinValue=0;
public long Sum=0;
public double Average=0;
}
}
相关文章推荐
- [C#学习笔记之多线程1]使用ParameterizedThreadStart委托向线程函数传送参数
- 带参数线程的操作(ParameterizedThreadStart,ThreadStart)
- 编译错误 : 某函数 的重载均与委托“System.Threading.ParameterizedThreadStart”不匹配
- c#怎样利用this.Invoke()方法,用委托调用带参数的方法?new ParameterizedThreadStart()除外
- C# multithread,threadpool,ParameterizedThreadStart,传递参数给线程以及回调
- ParameterizedThreadStart,ThreadStart的使用,线程Thread传参数
- .Net2.0 的线程 ParameterizedThreadStart &BackgroundWorker
- .Net2.0 的新线程 ParameterizedThreadStart &BackgroundWorker
- 可变参数函数的使用:va_start() , va_end() , va_list
- 关于带返回值委托函数和委托函数使用引用参数的问题
- 可变参数的函数 start va_arg va_end 的使用和原理
- Qt 的线程与事件循环——可打印threadid进行观察槽函数到底是在哪个线程里执行,学习moveToThread的使用)
- CreateRemoteThread函数多参数传入使用方法
- va_start、va_end、va_list的使用 函数参数个数不确定的情况
- java 线程 Thread 使用介绍,包含wait(),notifyAll() 等函数使用介绍
- 关于python使用threadpool中的函数单个参数和多个参数用法举例
- [转贴]ParameterizedThreadStart,ThreadStart的使用,线程Thread传参数
- .Net2.0 的新线程 ParameterizedThreadStart
- .Net2.0 的新线程 ParameterizedThreadStart
- 线程中通过信号和槽函数传递信息的时候,由于用到了自己定义的参数结构,所以使用qRegisterMetaType