大家帮我解决一个关于winform问题,至于关闭窗体后仍显示调试状态。
这个是执行窗体
using System;这是服务器类代码
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace 客户端
{
class 服务器
{
//声明一个int类型常量存放端口号
private const int listenPort = 11000;
public string strInfo=null;
public bool boo = true;
public void StartListener()
{
//实例化UdpClient类,接收值为端口号
UdpClient listener = new UdpClient(listenPort);
//实例化类IPEndPoint,用listenPort的端口来接收来自所有ip的信息
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
//写一个死循环监控
while (boo)
{
//声明一个byte类型数组,存放接收过来的值。
byte[] bytes = listener.Receive(ref groupEP); //ref 关键字使参数按引用传递
//获得发信人的IP
string strIP = "信息来自" + groupEP.Address.ToString();
//获得信息通过gb2312编码转化为汉字
strInfo = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
//return strInfo;
//MessageBox.Show(strInfo, strIP);
}
}
catch (Exception e)
{
strInfo = e.ToString();
//return e.ToString();
}
finally
{
listener.Close();
}
}
}
}
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.Sockets;
using System.Net;
using System.Threading;
namespace 客户端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread thread = null;
private Thread thread1 = null;
public void Send()
{
//实例化Socket对象,指定 Socket 类的实例可以使用的寻址方案,Socket 类的实例类型以及协议类型
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//用IPAddress类型的broadcast来接收文本框中的ip
IPAddress broadcast = IPAddress.Parse(textBox1.Text);
//声明一个byte类型数组,存放要发送的值。通过gb2312编码转换为特殊字符
byte[] sendbuf = Encoding.GetEncoding("gb2312").GetBytes(this.textBox2.Text.ToString());
//实例化类IPEndPoint,指定ip地址以及端口号
IPEndPoint ep = new IPEndPoint(broadcast, 11000);
//发送信息
s.SendTo(sendbuf, ep);
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
{
Send();
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + textBox2.Text + "\r\n";
textBox2.Clear();
}
else
{
MessageBox.Show("输入内容为空!");
}
}
服务器 fwq = new 服务器();
private void Form1_Load(object sender, EventArgs e)
{
//获取本机ip
textBox1.Text = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
thread = new Thread(new ThreadStart(fwq.StartListener));
thread.Start();
thread1 = new Thread(new ThreadStart(jiankong));
thread1.Start();
//textBox1.Text= Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
//MessageBox.Show(fwq.StartListener());
}
private void button2_Click(object sender, EventArgs e)
{
if (fwq.strInfo != null)
{
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + fwq.strInfo + "\r\n";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Application.ExitThread();
fwq.boo = false;
thread.Abort();
thread1.Abort();
this.Dispose();
}
string val = null;
public delegate void SetTextHandler(string text);
public void jiankong()
{
bool b = true;
while (b)
{
if (fwq.strInfo != null&&fwq.strInfo!=val)
{
if (textBox3.InvokeRequired)
{
SetTextHandler d = new SetTextHandler(SetText);
//this.invoke(d, new object[] { fwq.strInfo });
this.Invoke(d, new object[] { fwq.strInfo });
val = fwq.strInfo;
}
else
{
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + fwq.strInfo + "\r\n";
fwq.strInfo = null;
val = fwq.strInfo;
}
}
}
}
void SetText(string zhi)
{
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + fwq.strInfo + "\r\n";
}
}
}
谢谢各位朋友。
[最优解释]
添加FormClosing事件,在其中添加代码:
System.Environment.Exit(0); //结束进程时,关闭所有线程
试试看
[其他解释]
在form 的 closing 事件里加入
Environment.Exit(0);
[其他解释]
FORM关闭并不会输出关闭app的中断。
欲知详情,请熟读《windows程序设计》
[其他解释]
窗体只是个窗体,进程是进城,代码没执行完,自然还在调试状态。一个hello world程序,从你关闭窗体那一刻到程序退出还要N多代码要执行。
[其他解释]
什么管窗体处于调试状态,说明白点
[其他解释]
他的意思是调试的时候关闭form之后,vs还是显示“正在调试”
有时候有别的线程正在运行没有结束也会导致这种情况
[其他解释]
很正常,关闭窗体又不一定整个application都结束了
[其他解释]
设置线程为后台线程
[其他解释]
感觉还落了很多知识点!!!
[其他解释]
理解万岁,知音难觅。
[其他解释]
嗯啊!谢谢,我试试!
[其他解释]
具体需要怎么做!?求思路。