public void Beta()
{
Console.WriteLine("Smile");
}ThreadStart thr_start_func = new ThreadStart(Beta);
Thread fThread = new Thread(thr_start_func);
fThread.Start();
//上面一段程序可以编译通过,可是下面的就不行public void Beta(int a)
{
Console.WriteLine("Smile");
}ThreadStart thr_start_func = new ThreadStart(Beta(2));
Thread fThread = new Thread(thr_start_func);
fThread.Start();
//这里就出现问题啦 ,我只能使用没有参数的函数,但是一旦使用有参数的函数就报错,我应该怎么解决,谢谢大家。

解决方案 »

  1.   

    用ParameterizedThreadStart。        Thread newThread = new Thread(
                new ParameterizedThreadStart(Beta));
            
            // 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(2);
      

  2.   

    楼上正解,ThreadStart 只能是无参数的,如果需要带参数 就要用ParameterizedThreadStart
      

  3.   

    Thread th=new Thread(new ThreadStart(BEAT))
    private void BEAT()
    {
    Beat(a);
    }
      

  4.   

    1楼正解
    带参数的用ParameterizedThreadStart
      

  5.   

    ParameterizedThreadStart这个不知道 学习了  我的方法也行 我自己研究的
      

  6.   

    教你四种传参数的法方[1]通过构造函数传递
    using System;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;namespace PCTools
    {
        class Test
        {
            static void Main(string[] args)
            {
                Console.WriteLine("========Test Begin=========");            //Create 
                B b = new B("[1]构造函数传递法!");            //Start
                new Thread(b.Func).Start();            Console.WriteLine("===========Test End=========");
                Console.ReadKey();
            }
        }    class B
        {
            private string s;        public B(string s)
            {
                this.s = s;
            }        public void Func()
            {
                Console.WriteLine(s);
            }
        }}
      

  7.   

    [2]线程池传递参数法using System;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;namespace PCTools
    {
        class Test
        {
            static void Main(string[] args)
            {
                Console.WriteLine("========Test Begin=========");            //Create 
                B b = new B();            //Start
                ThreadPool.QueueUserWorkItem(new WaitCallback(b.Func), "[2]线程池传递参数法");            Console.WriteLine("===========Test End=========");
                Console.ReadKey();
            }
        }    class B
        {
            public void Func(string s)
            {
                Console.WriteLine(s);
            }        public void Func(object s)
            {
                this.Func((string)s);
            }
        }
    }
      

  8.   


           public void Beta(object a)
            {
                int i = (int)a;
                Console.WriteLine("Smile");
            }
             Thread fThread = new Thread(new ParameterizedThreadStart(Beta));
             fThread.Start(2);这样写就可以了
      

  9.   

    [3]回调法,并且能够处理返回值//注意:CallBack必须是static的using System;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;namespace PCTools
    {
        //delegate     class Test
        {
            delegate string MyMethodDelegate(string s);        static void Main(string[] args)
            {
                Console.WriteLine("========Test Begin=========");            //Create 
                B b = new B();            //Start
                MyMethodDelegate MyMethod = b.Func;
                MyMethod.BeginInvoke("[3]回调法,并且能够处理返回值", CallBack, null);            Console.WriteLine("===========Test End=========");
                Console.ReadKey();
            }        static public void CallBack(IAsyncResult result)
            {
                AsyncResult async = (AsyncResult)result;
                MyMethodDelegate DelegateInstance = (MyMethodDelegate)async.AsyncDelegate;            Console.WriteLine(DelegateInstance.EndInvoke(result));
            }
        }    class B
        {
            public string Func(string s)
            {
                return s;
            }
        }
    }
      

  10.   

    [4]处理单个参数]
    优点是:方法简洁,
    缺点是:要转换类型,最主要是不能处理多参数.[5]回调法,并且能够处理返回值","+多参数版"
    using System;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;namespace PCTools
    {
        //delegate     class Test
        {
            delegate string MyMethodDelegate(string s ,string t);        static void Main(string[] args)
            {
                Console.WriteLine("========Test Begin=========");            //Create 
                B b = new B();            //Start
                MyMethodDelegate MyMethod = b.Func;
                MyMethod.BeginInvoke("[5]回调法,并且能够处理返回值","+多参数版", CallBack, null);            //方法五
                Thread fThread = new Thread(b.Func);
                fThread.Start("[4]处理单个参数");            Console.WriteLine("===========Test End=========");
                Console.ReadKey();
            }        static public void CallBack(IAsyncResult result)
            {
                AsyncResult async = (AsyncResult)result;
                MyMethodDelegate DelegateInstance = (MyMethodDelegate)async.AsyncDelegate;            Console.WriteLine(DelegateInstance.EndInvoke(result));
            }
        }    class B
        {
            public string Func(string s, string t)
            {
                return s + t;
            }        public void Func(string s)
            {
                Console.WriteLine(s);
            }        public void Func(object s)
            {
                this.Func((string)s);
            }    }
    }