看书上写的晕乎晕乎的
大侠抛几个简单的例子

解决方案 »

  1.   

     new Thread(() => {
    // doSth
    }).Start();
      

  2.   

            private void donework()
            {
                Thread thread_dotcp = new Thread(new ThreadStart(AsynAccept));
                thread_dotcp.IsBackground = true;
                thread_dotcp.Start();
            }
     private void AsynAccept()
            {
                int receivecount = 0;
                try
                {
                    do
                    {                    if (null != mtcp && null != mtcp.Client && mtcp.Client.Connected)
                        {
                            NetworkStream ns = mtcp.GetStream();
                            byte[] receiveBytes = new byte[15];
                            EndPoint remoteEP = (remoteIP);
                            receivecount = mtcp.Client.ReceiveFrom(receiveBytes, 15, 0, ref remoteEP);
                            if (receivecount < 1)
                            {
                                System.Diagnostics.Debug.Print("连接退出!");
                                break;
                            }
                            StructModel.tcpSendTo tcpsendto = new StructModel.tcpSendTo();
                            tcpsendto.point = (IPEndPoint)mtcp.Client.RemoteEndPoint;
                            tcpsendto.oreceiveBytes = receiveBytes;
                            tcpsendto.tcpClient = mtcp;
                            base._ReBackClient = mtcp;
                            if (receivecount == 15 && receiveBytes[14] == 0)
                                System.Diagnostics.Debug.Print("收到数据包!");
                            else
                                if (receivecount == 15 && receiveBytes[14] == 1)
                                    System.Diagnostics.Debug.Print("收到心跳包!");
                                else
                                    System.Diagnostics.Debug.Print("收到测试包!");
                            DoWork(tcpsendto);
                            //_CRTUClient.LstReceiveData.Add(new StructModel.MemoryData(tcpsendto, receiveBytes));//receiveBytes
                            Thread.Sleep(100);
                        }                }
                    while (true);
                }
                catch
                {            }
                finally
                {
                   // Thread.CurrentThread.Abort();
                }
            }
      

  3.   

    有一堆任务要做,如1000份试卷要批。有N个老师,自然是这些老师一起批试卷。
    不可能一个老师自己做。这就是线程的概念。以下未编译过:public List<string> papers = new List<string>();// 找个地方初始化1000个string,代替试卷。
    Class Teacher
    {
      public void StartWork()
      {
             ThreadPool.QueueUserWorkItem(new WaitCallback(DoCheck),null);    
      }
    public void DoCheck(Object obj)
    {
        while(GetPaper() != "")
        {
            Console.WriteLine("do...");
            Thread.Sleep(10000);
        }
    }
    public Object obj = new Object();
    public string GetPaper()
    {
       lock(obj)
       {
          if(this.papers.Count>0)
          {
               string ret = this.papers[0];
               this.papers.RemoveAt(0);
               return ret;
          } 
       }
    }
    当你new了多个teacher后,
    public void Click()
    {
        Teacher t = new Teacher();
        t.StartWork();
    }
      

  4.   

    如果你是多核cpu,你写多线程做死循环,就会看见每个核都被占满,机器不能动。如果你是单线程死循环,就可以看见一个核被占满,机器还能动。如果你是单核cpu,不管多线程还是单线程,写个死循环就死在那里了。