我现在有一个线程每两秒钟像服务器,发送一个消息“s”,同时我要接收服务器返回一个“s”。还有一个线程,不确定在什么时候像服务器发送消息,发送完消息后服务器返回相应的消息,我同样需要接收。
我现在做了一个只是在不确定什么时候发送消息的线程,但是那个每两秒钟发送一个消息的线程我不知道应该怎么加。请师哥们指教。using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;namespace ThreadExample
{
    class Test
    {
        static TcpClient c;
        static StreamReader sr;
        static StreamWriter sw;
        static ArrayList ReadArry = new ArrayList();
        static void Main(string[] args)
        {
            c = new TcpClient();
            c.Connect("127.0.0.1", 7908);            sr = new StreamReader(c.GetStream(), Encoding.GetEncoding("GB2312"));
            sw = new StreamWriter(c.GetStream(), Encoding.GetEncoding("GB2312"));            Thread td = new Thread(new ThreadStart(Test.ReceiveData));
            td.Start();
            string str = "";
            while (str != "o")
            {
                SendMessage(sw, Console.ReadLine());
                System.Console.WriteLine("<<<<<<<");
            }
        }        public static void SendMessage(StreamWriter sw,string Msg)
        {
            string str = Msg;
            sw.WriteLine(str);
            sw.Flush();            //ReadArry = new ArrayList();
            ReadArry.Add(str);
        }        public static void GetMessage(string Msg)
        {
            /* 
             *如果在存放命令的可变数组里arry里,
             * 需要循环取服务器,返回字符串里的首字母,
             * 与存放输入命令的可变数字里arry相对应。
             */
            for (int j = 0; j < ReadArry.Count; j++)
            {
                if (ReadArry[j].ToString().Substring(0, 1) == Msg.Substring(0, 1))//判断返回的字符串与输入的命令是否匹配
                {
                    Console.WriteLine("成功");
                    ReadArry.RemoveAt(j);//如果匹配则把它从数组中删除
                }
            }
        }        public static void ReceiveData()
        {
            string s = "";
            while (true)
            {
                while ((s = sr.ReadLine()).Length > 0)
                {
                    MessageServer m = new MessageServer();
                    ArrayList arry = m.getMessage(s);
                    for (int i = 0; i < arry.Count; i++)
                    {
                        Console.WriteLine(arry[i].ToString());
                        GetMessage(arry[i].ToString());
                    }
                    //System.Console.WriteLine(s);
                }
            }
        }
    }
}

解决方案 »

  1.   

    再寫一個線程吧SendTime
    private void SendTime()
    {
       Thread.Sleep( 20000 );
       //發送數據;
    }別忘記using System.Threading;
      

  2.   

    再寫一個線程吧SendTime
    private void SendTime()
    {
       while( true )
       {
          Thread.Sleep( 20000 );
          //發送數據;
       }
    }別忘記using System.Threading;
      

  3.   

    TO:tjvictor(下一站----星星)
        
        那我在            Thread td = new Thread(new ThreadStart(Test.ReceiveData));
                td.Start();
    这里应该怎么应用那!
      

  4.   

    public AutoResetEvent auto=new AutoResetEvent(false);
    在你的receivedata()
    {
        while(true)
    {
         dosomething;
         auto.waitone(2000,false);
    }}保你成功!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  5.   

    TO:mabaolin(hello)    dosomething;这是什么意思!
      

  6.   

    public static void ReceiveData()
            {
                string s = "";
                while (true)
                {
                    Console.WriteLine(".................");
                    auto.WaitOne(2000, false);
                    while ((s = sr.ReadLine()).Length > 0)
                    {
                        MessageServer m = new MessageServer();
                        ArrayList arry = m.getMessage(s);
                        for (int i = 0; i < arry.Count; i++)
                        {
                            Console.WriteLine(arry[i].ToString());
                            GetMessage(arry[i].ToString());
                        }
                        //System.Console.WriteLine(s);
                    }
                }
            }我就这么加了啊!它并不是每两秒钟发一次消息啊!
      

  7.   

    look:
    http://www.hnce.net/Content/slick.238.html