日期:2014-05-17  浏览次数:20832 次

线程调用带参数的函数..求助
就是新建一个调用带参数的函数的线程,并把参数传递给函数 老是出问题


C# code

        private void RecThread(Socket client)
        {

            Thread Rec = new Thread(new ThreadStart(this.Receive));
            Rec.Start(client);
        }
        private void Receive(Socket client)
        {
         .
         .
         .
        }





------解决方案--------------------
Thread Rec = new Thread(new ThreadStart(delegate{Receive(参数)}));
可以吗

------解决方案--------------------
你可以通过自己写一个类来完成这个工作 。
C# code


 static void Main(string[] args)
 {

    SendMessage sm = new SendMessage(c);
    Thread sendThread = new Thread(new ThreadStart(sm.sendMessage));
    sendThread.Start();
 }
  public class SendMessage
    {
        private Socket s;

        public SendMessage(Socket param_socket)
        {
            this.s = param_socket;
        }

        public void sendMessage()
        {
            while(true)
            {
                string content = Console.ReadLine();
                if (content == "Close")
                {

                    Console.WriteLine("You have AFK!");
                    break;
                }
                else
                {
                    byte[] sendBytes = Encoding.ASCII.GetBytes(content);
                    s.Send(sendBytes, sendBytes.Length, 0);
                }  
            }
        }
    }