大家帮我解决一个关于winform问题,至于关闭窗体后仍显示调试状态。

Mr-Star 2012-11-26 02:43:44

这个是执行窗体
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";
}
}
}
这个是窗体代码
谢谢各位朋友。
...全文
254 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr-Star 2012-11-27
  • 打赏
  • 举报
回复
引用 6 楼 xillm0825 的回复:
设置线程为后台线程
具体需要怎么做!?求思路。
Mr-Star 2012-11-27
  • 打赏
  • 举报
回复
引用 3 楼 wm0508 的回复:
添加FormClosing事件,在其中添加代码: System.Environment.Exit(0); //结束进程时,关闭所有线程 试试看
嗯啊!谢谢,我试试!
Mr-Star 2012-11-27
  • 打赏
  • 举报
回复
引用 2 楼 hjywyj 的回复:
引用 1 楼 bdmh 的回复: 什么管窗体处于调试状态,说明白点 他的意思是调试的时候关闭form之后,vs还是显示“正在调试” 有时候有别的线程正在运行没有结束也会导致这种情况
理解万岁,知音难觅。
  • 打赏
  • 举报
回复
窗体只是个窗体,进程是进城,代码没执行完,自然还在调试状态。一个hello world程序,从你关闭窗体那一刻到程序退出还要N多代码要执行。
卧_槽 2012-11-26
  • 打赏
  • 举报
回复
FORM关闭并不会输出关闭app的中断。 欲知详情,请熟读《windows程序设计》
liuxin_0725 2012-11-26
  • 打赏
  • 举报
回复
感觉还落了很多知识点!!!
「已注销」 2012-11-26
  • 打赏
  • 举报
回复
设置线程为后台线程
深海之蓝 2012-11-26
  • 打赏
  • 举报
回复
在form 的 closing 事件里加入 Environment.Exit(0);
ZpSoft_wlp 2012-11-26
  • 打赏
  • 举报
回复
很正常,关闭窗体又不一定整个application都结束了
wm0508 2012-11-26
  • 打赏
  • 举报
回复
添加FormClosing事件,在其中添加代码: System.Environment.Exit(0); //结束进程时,关闭所有线程 试试看
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
什么管窗体处于调试状态,说明白点
他的意思是调试的时候关闭form之后,vs还是显示“正在调试” 有时候有别的线程正在运行没有结束也会导致这种情况
bdmh 2012-11-26
  • 打赏
  • 举报
回复
什么管窗体处于调试状态,说明白点

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧