C#传递参数到线程的n个方法

[转]http://kb.cnblogs.com/a/888688/

本片文章的议题是有关于传递参数到线程的几种方法。

首先我们要知道什么是线程,什么时候要用到线程,如何去使用线程,如何更好的利用线程来完成工作。
线程是程序可执行片段的最小单元,是组成运行时程序的基本单元,一个进程有至少一个线程组成。一般在并行处理等待事件的时候要用到线程,如等待网络响应,等待I/O通讯,后台事务处理等情况。使用线程其实很简单,在.net框架下面你首先要定义一个函数来完成一些工作,然后实例化一个线程对象Thread thrd = new Thread(new ThreadStart(线程函数)); 其中ThreadStart是一个不带参数的函数委托。最后使用thrd.Start()就可以启动线程了。当然这只是一个很简单的例子,实际中使用线程会有很多的问题要解决,比如传递参数到线程中,等待线程返回,如何同步线程进行同一资源访问,如何防止死锁和竞争条件,如何有效的利用线程池,如何提供线程效率等问题。本文在这里将只对传递参数到线程进行探讨。

在上面举例中我们看到线程实例化的参数是一个不带任何参数的函数委托,那么就证明了我们不可能通过这样一个委托传递参数到线程内部。那么我们该怎么做呢?就我目前所知有三种方法可以实现:1. 利用线程实现类,将调用参数定义成属性的方式来操作线程参数;2. 利用ParameterizedThreadStart委托来传递输入参数;3. 利用线程池来实现参数传入。

1. 创建线程实现类,这种方式有个最大的优点就是可以通过线程返回多个返回值
假设存在在一个打印功能的线程,通过主函数传入打印的行,列和字符数据,并返回打印的字符总数。
    class ThreadOutput
    {
        int _rowCount = 0;
        int _colCount = 0;
        char _char = '*';
        int _ret = 0;

        /**//// <summary>
        /// 输入参数
         /// </summary>
        public int RowCount
        {
            set { _rowCount = value; }
        }

        public int ColCount
        {
            set { _colCount = value; }
        }

        public char Character
        {
            set { _char = value; }
        }

        /**//// <summary>
        /// 输出参数
         /// </summary>
        public int RetVal
        {
            get { return _ret; }
        }

        public void Output()
        {
            for (int row = 0; row < _rowCount; row++)
            {
                for (int col = 0; col < _colCount; col++)
                {
                    Console.Write("{0} ", _char);
                    _ret++;
                }
                Console.Write(" ");
            }
        }


            ThreadOutput to1 = new ThreadOutput();
            to1.RowCount = 10;
            to1.ColCount = 20;

            Thread thrd = new Thread(new ThreadStart(to1.Output));
            // 设置为后台线程,主要是为不影响主线程的结束
            thrd.IsBackground = true;
            thrd.Start();最后要注意的是由于线程实现类是通过属性来传递数值的,那么在属性访问器中要进行线程同步,否则取得的值可能不正确。

2. 利用ParameterizedThreadStart委托来传递输入参数
ParameterizedThreadStart委托是.Net2.0中才有的。该委托提供来在启动线程时传递一个object参数到线程中。这种方式使用起来比较简单,但是由于需要对object对象进行类型转换,所以存在类型不一致的隐患。

3. 利用线程池来实现参数传入
什么是线程池,线程池是系统提供一个存放线程的容器,进入线程池后的线程控制权由系统掌握。利用线程池的好处是我们无需为线程存在大量空闲时间而去思考干点别的什么,适合于常规性的事务处理。在.Net中线程池是一个static类,所以我们需要通过ThreadPool来调用相关的函数。其中向线程池中加入线程的函数就是ThreadPool.QueueUserWorkItem(new WaitCallBack(), object obj)。这个函数有个WaitCallBack的委托,[ComVisibleAttribute(true)] 
public delegate void WaitCallback(Object state)
通过这个委托我们也可以传递参数到线程中。

其实还有一种方法可以传递参数到线程中,那就是通过回调函数来实现,只不过这种方法本质上和第一种通过类的数据成员来传递参数的机制一样。所以就不再细说来!

具体的实现可以参考下面的源代码:

全部源代码如下:(vs 2005编译通过)
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading;
5
6namespace UsingThread
7{
8    struct RowCol
9    {
10        public int row;
11        public int col;
12    };
13
14    class ThreadOutput
15    {
16        // 建立等待时间
17        static public ManualResetEvent prompt = new ManualResetEvent(false);
18
19        int _rowCount = 0;
20        int _colCount = 0;
21        char _char = '*';
22        int _ret = 0;
23
24        /**//// <summary>
25        /// 输入参数
26        /// </summary>
27        public int RowCount
28        {
29            set { _rowCount = value; }
30        }
31
32        public int ColCount
33        {
34            set { _colCount = value; }
35        }
36
37        public char Character
38        {
39            set { _char = value; }
40        }
41
42        /**//// <summary>
43        /// 输出参数
44        /// </summary>
45        public int RetVal
46        {
47            get { return _ret; }
48        }
49
50        public void Output()
51        {
52            for (int row = 0; row < _rowCount; row++)
53            {
54                for (int col = 0; col < _colCount; col++)
55                {
56                    Console.Write("{0} ", _char);
57                    _ret++;
58                }
59                Console.Write(" ");
60            }
61            // 激活事件
62            prompt.Set();
63        }
64
65        public void Output(Object rc)
66        {
67            RowCol rowCol = (RowCol)rc;
68            for (int i = 0; i < rowCol.row; i++)
69            {
70                for (int j = 0; j < rowCol.col; j++)
71                    Console.Write("{0} ", _char);
72                Console.Write(" ");
73            }
74        }
75    }
76
77    class Program
78    {
79        static void Main(string[] args)
80        {
81            ThreadOutput to1 = new ThreadOutput();
82            to1.RowCount = 10;
83            to1.ColCount = 20;
84
85            Thread thrd = new Thread(new ThreadStart(to1.Output));
86            // 设置为后台线程,主要是为不影响主线程的结束
87            thrd.IsBackground = true;
88            thrd.Start();
89
90            // 建立事件等待
91            ThreadOutput.prompt.WaitOne();
92
93            Console.WriteLine("{0}", to1.RetVal);
94
95            ThreadOutput to2 = new ThreadOutput();
96            to2.RowCount = 5;
97            to2.ColCount = 18;
98            to2.Character = '^';
99            Thread thrds = new Thread(new ThreadStart(to2.Output));
100            thrds.IsBackground = true;
101            thrds.Start();
102            thrds.Join();
103
104            Console.WriteLine("{0}", to2.RetVal);
105
106            // 传递参数给线程的另一种实现
107            RowCol rc = new RowCol();
108            rc.row = 12;
109            rc.col = 13;
110            to1.Character = '@';
111            if (ThreadPool.QueueUserWorkItem(new WaitCallback(to1.Output), rc))
112                Console.WriteLine("Insert Pool is success!");
113            else
114                Console.WriteLine("Insert Pool is failed!");
115            Thread.Sleep(1000);
116
117
118            // 使用新的ThreadStart委托来传递参数
119            rc.col = 19;
120            to2.Character = '#';
121            Thread thrdt = new Thread(new ParameterizedThreadStart(to2.Output));
122            thrdt.Start(rc);
123            thrdt.Join();
124        }
125
126    }
127}
128

原文地址:https://www.cnblogs.com/gc2013/p/3842259.html