本帖最后由 ken200344 于 2013-01-15 11:04:34 编辑

解决方案 »

  1.   

    因为都是线程0.0所以才全部代码都贴上来了
    出错的是:
         private void button2_Click(object sender, EventArgs e)
            {
                receiveCount = int.Parse(textBox2.Text);
                toolStripProgressBar1.Maximum = receiveCount;
                toolStripProgressBar1.Value = 0;
                Thread threadReceive = new Thread(ReceiveMessage);
                threadReceive.Start();
            }        private void ReceiveMessage()
            {
                statusStrip1.Invoke(shwStatusInfoCallBack, "接受中...");
                for (int i = 0; i < receiveCount; i++)
                {
                    try
                    {
                        string rcvMsgStr = br.ReadString();
                        //statusStrip1.Invoke(shwProgressProcCallBack, i + 1);
                        if (rcvMsgStr != null)
                        {
                            listBox1.Invoke(shwMsgforViewCallBack, rcvMsgStr);
                        }
                    }
                    catch(Exception e)
                    {
                        MessageBox.Show(e.ToString());
                        if (br != null)
                        {
                            br.Close();
                        }
                        if (bw != null)
                        {
                            bw.Close();
                        }
                        if (tcpClient != null)
                        {
                            tcpClient.Close();
                        }
                        //statusStrip1.Invoke(shwStatusInfoCallBack, "断开连接!");
                        //statusStrip1.Invoke(shwProgressProcCallBack, 0);
                        DateTime now = DateTime.Now;
                        while (now.AddSeconds(2) > DateTime.Now) { }
                        Thread threadAccept = new Thread(AcceptClientConnect);
                        threadAccept.Start();
                        break;
                    }
                }
                statusStrip1.Invoke(shwStatusInfoCallBack, "接受了" + receiveCount + "条信息. ");
            }
      

  2.   

    因为都是线程0.0所以才全部代码都贴上来了
    出错的是:
         private void button2_Click(object sender, EventArgs e)
            {
                receiveCount = int.Parse(textBox2.Text);
                toolStripProgressBar1.Maximum = receiveCount;
                toolStripProgressBar1.Value = 0;
                Thread threadReceive = new Thread(ReceiveMessage);
                threadReceive.Start();
            }        private void ReceiveMessage()
            {
                statusStrip1.Invoke(shwStatusInfoCallBack, "接受中...");
                for (int i = 0; i < receiveCount; i++)
                {
                    try
                    {
                        string rcvMsgStr = br.ReadString();
                        //statusStrip1.Invoke(shwProgressProcCallBack, i + 1);
                        if (rcvMsgStr != null)
                        {
                            listBox1.Invoke(shwMsgforViewCallBack, rcvMsgStr);
                        }
                    }
                    catch(Exception e)
                    {
                        MessageBox.Show(e.ToString());
                        if (br != null)
                        {
                            br.Close();
                        }
                        if (bw != null)
                        {
                            bw.Close();
                        }
                        if (tcpClient != null)
                        {
                            tcpClient.Close();
                        }
                        //statusStrip1.Invoke(shwStatusInfoCallBack, "断开连接!");
                        //statusStrip1.Invoke(shwProgressProcCallBack, 0);
                        DateTime now = DateTime.Now;
                        while (now.AddSeconds(2) > DateTime.Now) { }
                        Thread threadAccept = new Thread(AcceptClientConnect);
                        threadAccept.Start();
                        break;
                    }
                }
                statusStrip1.Invoke(shwStatusInfoCallBack, "接受了" + receiveCount + "条信息. ");
            }