C#实现线程调用带参数的方法
2013-01-12 15:23
525 查看
推荐方法:将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。
具体代码如下(本示例来自MSDN)
using System;
using System.Threading;
//ThreadWithState 类里包含了将要执行的任务以及执行任务的方法
public class ThreadWithState {
//要用到的属性,也就是我们要传递的参数
private string boilerplate;
private int value;
//包含参数的构造函数
public ThreadWithState(string text, int number)
{
boilerplate = text;
value = number;
}
//要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用
public void ThreadProc()
{
//这里就是要执行的任务,本处只显示一下传入的参数
Console.WriteLine(boilerplate, value);
}
}
//用来调用上面方法的类,是本例执行的入口
public class Example {
public static void Main()
{
//实例化ThreadWithState类,为线程提供参数
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.", 42);
// 创建执行任务的线程,并执行
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
}
}
[b]不推荐方法:[/b]使用ParameterizedThreadStart。
调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传递给线程。
使用 ParameterizedThreadStart 委托不是传递数据的类型安全的方法,因为 System.Threading.Thread.Start(System.Object) 方法重载接受任何对象。
ParameterizedThreadStart ParStart = new ParameterizedThreadStart(ThreadMethod);
Thread myThread = new Thread(ParStart);
object o = "hello";
myThread.Start(o);
//ThreadMethod如下:
public void ThreadMethod(object ParObject)
{
//程序代码
}
具体代码如下(本示例来自MSDN)
using System;
using System.Threading;
//ThreadWithState 类里包含了将要执行的任务以及执行任务的方法
public class ThreadWithState {
//要用到的属性,也就是我们要传递的参数
private string boilerplate;
private int value;
//包含参数的构造函数
public ThreadWithState(string text, int number)
{
boilerplate = text;
value = number;
}
//要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用
public void ThreadProc()
{
//这里就是要执行的任务,本处只显示一下传入的参数
Console.WriteLine(boilerplate, value);
}
}
//用来调用上面方法的类,是本例执行的入口
public class Example {
public static void Main()
{
//实例化ThreadWithState类,为线程提供参数
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.", 42);
// 创建执行任务的线程,并执行
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
}
}
[b]不推荐方法:[/b]使用ParameterizedThreadStart。
调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传递给线程。
使用 ParameterizedThreadStart 委托不是传递数据的类型安全的方法,因为 System.Threading.Thread.Start(System.Object) 方法重载接受任何对象。
ParameterizedThreadStart ParStart = new ParameterizedThreadStart(ThreadMethod);
Thread myThread = new Thread(ParStart);
object o = "hello";
myThread.Start(o);
//ThreadMethod如下:
public void ThreadMethod(object ParObject)
{
//程序代码
}
相关文章推荐
- C#线程调用带参数的方法
- C#线程调用带参数的方法
- 转:C#线程调用带参数的方法
- C#线程调用带参数的方法 ~
- 转----C#线程调用带参数的方法 ~
- C#线程调用带参数的方法 ~
- [C#]线程调用带参数的方法 转
- C#线程调用带参数的方法 ~
- C#线程调用带参数的方法 ~
- 要实现线程调用带参数的方法有两种办法。
- C#线程调用带参数的方法
- C#线程调用带参数的方法,给控件赋值
- 转:C#线程调用带参数的方法
- [转]C#线程调用带参数的方法
- C#线程进程总结(线程调用带参数的方法)
- C#线程调用带参数的方法 ~
- C#线程调用带参数的方法
- C#线程调用带参数的方法
- c#带参数和返回值的函数 开启线程调用的方法
- C#构建多线程应用程序(1) —— 使用委托来自动创建次线程以实现异步方法的调用