using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;//程序中使用到编码
using System.Windows.Forms;
//add  2009-2-16using System.Net;
using System.Net.Sockets;
using System.Threading;//程序中使用到线程
namespace UDP对时服务器端
{
    public partial class Form1 : Form
    {
        private UdpClient server ;
        private IPEndPoint receivePoint ;
        private int port = 8080 ; //定义端口号
        private string ip =  "127.0.0.1" ;//设定本地IP地址
        private Thread startServer ;        public Form1()
        {
            InitializeComponent();
        }
        public void start_server ( )
        {
        while ( true )
        {
        //接收从远程主机发送到本地8080端口的数据
        byte[] recData = server.Receive ( ref receivePoint ) ;
        ASCIIEncoding encode = new ASCIIEncoding ( ) ;
        //获得客户端请求数据
        string Read_str = encode.GetString ( recData ) ;
        //提取客户端的信息,存放到定义为temp的字符串数组中
        string[] temp = Read_str.Split('/') ;
        //显示端口号的请求信息
                listBox1.Items.Add("时间:" + DateTime.Now.ToLongTimeString() + "   接收信息如下:");
                listBox1.Items.Add("客户机:" + temp[0]);
                listBox1.Items.Add("端口号:" + temp[1]);

        //发送服务器端时间和日期
        byte[] sendData =encode.GetBytes ( System.DateTime.Now.ToString ( ) ) ;                listBox1.Items.Add("发送服务器时间!");         //对远程主机的指定端口号发送服务器时间
        server.Send ( sendData , sendData.Length , temp[0] , Int32.Parse ( temp[1] ) ) ;
        }
        }
        public void run ( )
        {
        //利用本地8080端口号来初始化一个UDP网络服务
        server = new UdpClient ( port ) ;
            IPAddress a = IPAddress.Parse(ip);
        receivePoint = new IPEndPoint ( a , port ) ;
        //开一个线程
        startServer = new Thread ( new ThreadStart ( start_server ) ) ;
        //启动线程
        startServer.Start ( ) ;
        }        private void button1_Click(object sender, EventArgs e)
        {
         //清除服务器端程序日志
            listBox1.Items.Clear ( ) ;        }        private void Form1_Load(object sender, EventArgs e)
        {
             //启动对时服务
            run ( ) ;        }
    }
}
红色部分出现问题,删去则可以,
如何解决在一个线程中往主线程窗体中列表框写入信息?

解决方案 »

  1.   

    public Form1()
            {
                InitializeComponent();
                Check................... = false;
            } check开头的一个属性  具体既不清楚了
      

  2.   

      public  delegate void AddString(string Mess);
           public void Add(string Mess)
           {
               if (listBox1.InvokeRequired)
               {
                   AddString add = new AddString(Add);
                   listBox1.Invoke(Add, new object[] { Mess });
               }
               else
               {
                   listBox1.Items.Add(Mess);
               }
           }
    调用Add添加
      

  3.   

     public  delegate void AddString(string Mess); 
          public void Add(string Mess) 
          { 
              if (listBox1.InvokeRequired) 
              { 
                  AddString addmess = new AddString(Add); 
                  listBox1.Invoke(addmess, new object[] { Mess }); 
              } 
              else 
              { 
                  listBox1.Items.Add(Mess); 
              } 
          } 
      

  4.   

    Control.CheckForIllegalCrossThreadCalls = false;
      

  5.   

    原因是startServer 不是持有listBox1控件的线程,必须调用listBox1的Invoke方法或BeginInvoke方法,封送到主线中去
      

  6.   

    listBox1.Invoke每个ui控件都有自己的跨线程访问的委托方法。。也就是Invoke