线程能不能传参数进去
我看拉些文章说不能传进去,可是我发现thread有3个重载方法.有个好象就是可以用参数的啊

解决方案 »

  1.   

    你说的是这个吧...public Thread (ParameterizedThreadStart start)...给你个例子吧:using System;
    using System.Threading;public class Work
    {
        public static void Main()
        {
            // To start a thread using a shared thread procedure, use
            // the class name and method name when you create the 
            // ParameterizedThreadStart delegate.
            //
            Thread newThread = new Thread(
                new ParameterizedThreadStart(Work.DoWork));
            
            // Use the overload of the Start method that has a
            // parameter of type Object. You can create an object that
            // contains several pieces of data, or you can pass any 
            // reference type or value type. The following code passes
            // the integer value 42.
            //
            newThread.Start(42);        // To start a thread using an instance method for the thread 
            // procedure, use the instance variable and method name when 
            // you create the ParameterizedThreadStart delegate.
            //
            Work w = new Work();
            newThread = new Thread(
                new ParameterizedThreadStart(w.DoMoreWork));
            
            // Pass an object containing data for the thread.
            //
            newThread.Start("The answer.");
        }
     
        public static void DoWork(object data)
        {
            Console.WriteLine("Static thread procedure. Data='{0}'",
                data);
        }    public void DoMoreWork(object data)
        {
            Console.WriteLine("Instance thread procedure. Data='{0}'",
                data);
        }
    }/* This code example produces the following output (the order 
       of the lines might vary):Static thread procedure. Data='42'
    Instance thread procedure. Data='The answer'
    */
      

  2.   

    我说的不是这个啊.这个是2005里的.我是说2003中的啊
     Thread newThread =new Thread(,,,)
    我记得最后的参数是objedt,好就没有用了.
      

  3.   

    Vs2003和2005对于Thread的定义不一样吗?这个我就不清楚了,一直用的是Vs2005...
      

  4.   

    liujia_0421() ( ) 信誉:100    Blog 请问:
    ---------------------------------------------------------------------
    我在2003中没有找到这个委托:ParameterizedThreadStart 
    它是C#定义还是自定义的?
    我没用过2005,不知道这个传参的Start方法是不是可靠。
      

  5.   

    TO:我在2003中没有找到这个委托:ParameterizedThreadStart 
    它是C#定义还是自定义的?
    我没用过2005,不知道这个传参的Start方法是不是可靠。C#定义的...这个我也没怎么用过,我一般都用不传参的ThreadStart...
      

  6.   

    ParameterizedThreadStart 委托和 Thread.Start(Object) 方法重载使得将数据传递给线程过程变得简单,但由于可以将任何对象传递给 Thread.Start(Object),因此这种方法并不是类型安全的。将数据传递给线程过程的一个更可靠的方法是将线程过程和数据字段都放入辅助对象。
      

  7.   

    using System;
    using System.Threading;public class ThreadWithState {
        private string boilerplate;
        private int value;    public ThreadWithState(string text, int number) 
        {
            boilerplate = text;
            value = number;
        }    public void ThreadProc() 
        {
            Console.WriteLine(boilerplate, value); 
        }
    }public class Example {
        public static void Main() 
        {
            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.");  
        }
    }
      

  8.   

    当然可以,简单的方法就是你把thread的delegate那个函数封在类里面,然后加点成员变量什么的。
      

  9.   

    呵呵
    zyip给大家讲一个世界上最简单也最复杂的方法,匿名委托:
    Thread t = new Thread(delegate() { proc(10,obj); });
    t.Start();private void proc(int a,ref obj)
    {
    ......
    }这的用法很简单,理论比较复杂,照用就行
      

  10.   

    Thread t = new Thread(delegate() { proc(10,ref obj); });
    t.Start();private void proc(int a,ref obj)
    {
    ......
    }错了,刚才少了个ref