using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace 聊天__qq群
{
    public partial class Form1 : Form
    {        bool done = false;
       
        public Form1()
        {
            InitializeComponent();
        }
        private void StartListener()
        {
            UdpClient listener = new UdpClient(int.Parse(textBox1.Text.ToString().Trim())); //使用UDP协议  
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(textBox1.Text.ToString().Trim())); //任意IP,
            try
            {
                while (!done)//使用永真循环另其一直处于监听状态
                {                    byte[] bytes = listener.Receive(ref groupEP);
                    string strIP;
                    strIP = "信息来自" + groupEP.Address.ToString();//获得发信人的IP
                    string strInfo = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);//获得信息
                    MessageBox.Show(strInfo, strIP);
                }
            }
            catch (Exception e)
            { Console.WriteLine(e.ToString()); }
            finally { listener.Close(); }        }
        private void Form1_Load(object sender, EventArgs e)
        {           
        }        private void button1_Click(object sender, EventArgs e)
        {
            StartListener(); //调用监听方法
        }       
    }
}
C#一直监听

解决方案 »

  1.   

    ThreadPool.QueueUserWorkItem(StartListener, null);
      

  2.   


    private void button1_Click(object sender, EventArgs e)
            {
               ThreadPool.QueueUserWorkItem(StartListener, null); //调用监听方法
            }好像是楼上的意思
      

  3.   


    private void button1_Click(object sender, EventArgs e)
            {
               ThreadPool.QueueUserWorkItem(StartListener, null); //调用监听方法
            }好像是楼上的意思 不行啊  运行出现错误了
      

  4.   

    你在主线程写死循环必然会被卡死
    ThreadPool.QueueUserWorkItem(StartListener, null);替换StartListener();
      

  5.   

    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;注意:要加上这句。 在这加吧。public Form1()
            {
                InitializeComponent();
                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            }
      

  6.   

     我换了但是  出现 错误 1 与“System.Threading.ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)”最匹配的重载方法具有一些无效参数 d:\Users\yuanzhenhua\Documents\Visual Studio 2008\Projects\聊天  qq群\聊天  qq群\Form1.cs 53 13 聊天  qq群
     的错误啊
      

  7.   

    建议学习一下多线程编程。
    你在主线程上使用死循环肯定会导致窗体卡死。
    建议换成Timer控件,或使用另外一个线程执行。
      

  8.   

    private void StartListener()改成private void StartListener(object o)
      

  9.   

    你那个方法加个参数private void StartListener(object obj)
      

  10.   

    你那个方法加个参数private void StartListener(object obj)
    这个参数下面要写什么东西吗
      

  11.   

    你那个方法加个参数private void StartListener(object obj)
    这个参数下面要写什么东西吗  谢谢啊  解决了 谢谢大神
      

  12.   

    你那个方法加个参数private void StartListener(object obj)
    这个参数下面要写什么东西吗
    你可以不用你那个方法加个参数private void StartListener(object obj)
    这个参数下面要写什么东西吗参数你可以不用,但是一定要写。public delegate void WaitCallback(object state);