使用delegate给线程传递参数

第一次在博客园写文章。

最近遇到一个问题,用到了多线程,以前用的时候线程启动时不需要传递参数,可现在需要时却被难了一把。。

还是先说说delegate

delegateC#中的一种类型,它实际上是一个能够持有对某个方法的引用的类。与其它的类不同,delegate类能够拥有一个签名(signature),并且它只能持有与它的签名相匹配的方法的引用。它所实现的功能与C/C++中的函数指针十分相似。它允许你传递一个类A的方法m给另一个类B的对象,使得类B的对象能够调用这个方法m。但与函数指针相比,delegate有许多函数指针不具备的优点。首先,函数指针只能指向静态函数,而delegate既可以引用静态函数,又可以引用非静态成员函数。在引用非静态成员函数时,delegate不但保存了对此函数入口指针的引用,而且还保存了调用此函数的类实例的引用。其次,与函数指针相比,delegate是面向对象、类型安全、可靠的受控(managed)对象。也就是说,runtime能够保证delegate指向一个有效的方法,你无须担心delegate会指向无效地址或者越界地址。(摘自:gerbil

      这一段先放着吧。

那如何在线程启动时传递参数呢?

先定义一个线程启动时的执行的方法:

        static void count(int i)
        
{
            Console.WriteLine(
"i={0", i);
        }

很简单的一个方法,显示传入int值。

启动线程的方法也很简单:

            int i = 4;
            
new System.Threading.Thread((System.Threading.ThreadStart)delegate { count(i); }).Start();

类似的,还可以用到TimerThreadPool
      ThreadPool中的应用:

System.Threading.ThreadPool.QueueUserWorkItem((System.Threading.WaitCallback)delegate { count(i); });

System.Threading.Timer中的应用

            System.Threading.Timer threadTimer = new System.Threading.Timer((System.Threading.TimerCallback)delegate { count(i); },null,0,30000);

System.Timers.Timer 中的应用

            System.Timers.Timer timersTimer = new System.Timers.Timer(30000);
            timersTimer.AutoReset 
= false;
            timersTimer.Elapsed 
+= new System.Timers.ElapsedEventHandler(delegate { count(i); });
            timersTimer.Enabled 
= true;
但是下面这段代码将输出些什么呢?
            for (int i = 0; i < 3; i++)
            
{
                System.Timers.Timer timersTimer 
= new System.Timers.Timer(30000);
                timersTimer.AutoReset 
= false;
                timersTimer.Elapsed 
+= new System.Timers.ElapsedEventHandler(delegate { count(i); });
                timersTimer.Enabled 
= true;
            }

            System.Threading.Thread.Sleep(
600000);

从代码的顺序上看应该输出:
i=0
i=1
i=2
再来看看实际的输出:
i=3
i=3
i=3

出现这种问题的原因就是上面提到的,delegate指定的只是一个地址,方法的入口地址,不但如此,方法变量也是指定的一个地址,当delegate代码还未执行时,变量所指定的地址的值已经改变,所以传入给方法的值也已经改变。

解决方法也很简单:把变量i变为私有变量。

            for (int i = 0; i < 3; i++)
            
{
                
int j = i;//在此处添了私有变量
                System.Timers.Timer timersTimer = new System.Timers.Timer(30000);
                timersTimer.AutoReset 
= false;
                timersTimer.Elapsed 
+= new System.Timers.ElapsedEventHandler(delegate { count(j); });
                timersTimer.Enabled 
= true;
            }

            System.Threading.Thread.Sleep(
600000);


再来看看输出结果:
i=0
i=1
i=2
posted @ 2008-05-29 09:51  zsea  阅读(2010)  评论(0编辑  收藏  举报