带参数的线程该怎么写?求高人给个例子

解决方案 »

  1.   

    class Program
        {        static void Main(string[] args)
            {            ThreadStart ts = new ThreadStart(delegate()
                {
                    (new ThreadClass("aaa")).RunMethod();
                });
                Thread thread = new Thread(ts);
                thread.Start();
            }        internal class ThreadClass
            {
                private string _a;
                public ThreadClass(string a)
                {
                    _a = a;
                }            public void RunMethod()
                {
                    Console.WriteLine(_a);
                }            
            }
               
        }
      

  2.   

    也可以用线程池,在添加工作队列的时候可以穿一个object对象作为参数,你的参数可以封装在里面,在启动线程时把object再强制转换为原有数据类型
      

  3.   

    using System;
    using System.Threading;public class Fibonacci
    {
        public Fibonacci(int n, ManualResetEvent doneEvent)
        {
            _n = n;
            _doneEvent = doneEvent;
        }    // Wrapper method for use with thread pool.
        public void ThreadPoolCallback(Object threadContext)
        {
            int threadIndex = (int)threadContext;
            Console.WriteLine("thread {0} started...", threadIndex);
            _fibOfN = Calculate(_n);
            Console.WriteLine("thread {0} result calculated...", threadIndex);
            _doneEvent.Set();
        }    // Recursive method that calculates the Nth Fibonacci number.
        public int Calculate(int n)
        {
            if (n <= 1)
            {
                return n;
            }        return Calculate(n - 1) + Calculate(n - 2);
        }    public int N { get { return _n; } }
        private int _n;    public int FibOfN { get { return _fibOfN; } }
        private int _fibOfN;    private ManualResetEvent _doneEvent;
    }public class ThreadPoolExample
    {
        static void Main()
        {
            const int FibonacciCalculations = 10;        // One event is used for each Fibonacci object
            ManualResetEvent[] doneEvents = new ManualResetEvent[FibonacciCalculations];
            Fibonacci[] fibArray = new Fibonacci[FibonacciCalculations];
            Random r = new Random();        // Configure and launch threads using ThreadPool:
            Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
            for (int i = 0; i < FibonacciCalculations; i++)
            {
                doneEvents[i] = new ManualResetEvent(false);
                Fibonacci f = new Fibonacci(r.Next(20,40), doneEvents[i]);
                fibArray[i] = f;
                ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
            }        // Wait for all threads in pool to calculation...
            WaitHandle.WaitAll(doneEvents);
            Console.WriteLine("All calculations are complete.");        // Display the results...
            for (int i= 0; i<FibonacciCalculations; i++)
            {
                Fibonacci f = fibArray[i];
                Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN);
            }
        }
    }
      

  4.   

    没有必要这样吧。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                Thread t = new Thread(Test);
                t.Start("23");
            }
            static void Test(object i)
            {
                Console.WriteLine(i);
            }
        }
    }
      

  5.   

    在 .NET Framework 2.0 版中,要实现线程调用带参数的方法有两种办法。第一种:使用ParameterizedThreadStart。调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传递给线程。使用 ParameterizedThreadStart 委托不是传递数据的类型安全的方法,因为 System.Threading.Thread.Start(System.Object) 方法重载接受任何对象。这种方法不推荐使用,故在此不做详细介绍,具体用法参见:http://msdn2.microsoft.com/zh-cn/library/system.threading.parameterizedthreadstart(VS.80).aspxParameterizedThreadStart 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.");  
        }
      

  6.   

    而且  用类 封装 就没有 体现 struck 的作用了
    这种小型 数据 用 struck 就足够了