所有代码都是在主窗体里面 只是点击一个按钮开启一个子线程调用主窗体的一个函数 该函数是起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;
}
}
}
}
然后还有一个停止按钮我想点击后停止该线程 也就是起关闭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;
}
}
}
}
解决方案 »
- 【求助】在winform项目中把datatable中数据导出到dbf文件
- 怎么实现一个控件在窗口上可以拖动?
- 如何调用阿里旺旺发送信息?
- winform窗体钥匙控件大小随窗体大小变化,如何控制控件的相对位置
- C#如何在没有安装驱动的情况下获取硬件的DeviceID或classGuid?
- 高手请进:在同一个类下修改系统托盘ContextMenu属性为何报错:未将对引用设置到对象的实例
- 请问,有没人知道用C#如何修改iis中的主机头?
- 求助c#下的分词系统和词库,多谢
- VS.NET 2003出现怪问题,既不能启动,又不能修复,还不让卸载。我彻底晕了!
- C#有base64的编码的包装类吗?是哪个?
- 紧急求助:CSDN左边导航栏中的树一片空白
- word文档与rpc服务器的关系
主要是为什么主线程的一个事件创造了一个线程 这个线程确实在运行 可是从主线程的其他事件访问该线程却得到的是null
而不是线程的实例 这个是为什么啊?
这句有问题,这里实际声明了一个叫ListenThread的btnStart_Click的局部变量,屏蔽了你窗口级的ListenThread。用
ListenThread = new Thread(new ThreadStart(this.StartListen));
就可以了。