所有代码都是在主窗体里面 只是点击一个按钮开启一个子线程调用主窗体的一个函数 该函数是起socket监听功能
然后还有一个停止按钮我想点击后停止该线程 也就是起关闭socket监听功能 所以我把那个子线程声明为窗体类的公共变量
public Thread ListenThread = null;  //监听子线程
这样都可以访问到 但是实际上开启了子线程 从任务管理器看也是的 跟踪的时候ListenThread 也不是null
可是点击停止按钮后ListenThread 在watch界面立刻变为null 为什么啊??代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace TcpServer
{
    public partial class Form1 : Form
    {
        #region 属性
        public TcpListener listener = null; //监听类
        public bool IsListen = false;
       public Thread ListenThread = null;  //监听子线程            
        #endregion        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            Thread.CurrentThread.Name = "FatherThread";
            cbxIP.Items.Clear();   
            //加载本机的IP地址
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in ips)
            {
                cbxIP.Items.Add(ip.ToString());
            }
        }        //点击后开启一个监听子线程进行监听
        private void btnStart_Click(object sender, EventArgs e)
        {
            listener = null;            if (cbxIP.Text.Trim() == "" || txtPort.Text.Trim() == "") return;            IPEndPoint localIP = new IPEndPoint(IPAddress.Parse(cbxIP.Text.Trim()), int.Parse(txtPort.Text.Trim()));
            listener = new TcpListener(localIP);
            listener.Start();            IsListen = true;            //子线程开始监听过程
            Thread ListenThread = new Thread(new ThreadStart(this.StartListen));
            ListenThread.Name = "SonThread";
            ListenThread.Start();                                               btnStop.Enabled = true;
            btnStart.Enabled = false;        }        private void StartListen()
        {
            while (IsListen)
            {
                TcpClient client = listener.AcceptTcpClient();                //建立连接
                ListViewItem item = new ListViewItem();
                item.SubItems["colTime"].Text = DateTime.Now.ToString();
                item.SubItems["colStyle"].Text = "命令";
                IPEndPoint remoteIP = (IPEndPoint)client.Client.RemoteEndPoint;
                item.SubItems["colIP"].Text = remoteIP.Address.ToString();
                item.SubItems["colPort"].Text = remoteIP.Port.ToString();
                item.SubItems["colMessage"].Text = "建立连接";                lvMessage.Items.Add(item);                client.Close();            }        }        //停止线程
        private void btnStop_Click(object sender, EventArgs e)
        {            if (ListenThread != null && ListenThread.IsAlive) //在这里ListenThread却是null 但是实际子线程是存在的
            {
                ListenThread.Abort();
                btnStart.Enabled = true;
            }            
            
        }
    }
}

解决方案 »

  1.   

    我上面那个加listview的代码有问题 会引起交叉线程错误 不过那个不是关键 
    主要是为什么主线程的一个事件创造了一个线程 这个线程确实在运行 可是从主线程的其他事件访问该线程却得到的是null
    而不是线程的实例 这个是为什么啊?
      

  2.   

    Thread   ListenThread   =   new   Thread(new   ThreadStart(this.StartListen)); 
    这句有问题,这里实际声明了一个叫ListenThread的btnStart_Click的局部变量,屏蔽了你窗口级的ListenThread。用
    ListenThread   =   new   Thread(new   ThreadStart(this.StartListen)); 
    就可以了。