C# 程序在关闭窗体后继续在进程中运行
最近,当使用我的 TCP/IP 服务器和客户端时,我注意到在我关闭它们后它们都在进程中保持打开状态。
我通过将套接字处理到服务器来修复客户端保持打开状态的问题,这会关闭框架后台的所有异步线程。
但是,当尝试对服务器执行此操作时,无论我做什么,该进程都会保持打开状态。
假设 TCP/IP 服务器正在处理它自己的东西,这段代码中是否还有其他东西可以保持进程打开?
编辑:更新: 如果我在 Program.cs 中的 Application.Run(new ServerForm()) 行之后放置一个断点,那么一旦我按 exit 或调用 Application.Exit() ,它就会中断。
我不知道是什么挂起了程序,但它没有退出主程序。
namespace ChatServer
{
public partial class ServerForm : Form
{
private NetLib.Server _server = new NetLib.Server();
public delegate void ClientConnection(ServerNetworkState ns);
public ServerForm()
{
InitializeComponent();
}
private void ServerForm_Load(object sender, EventArgs e)
{
//Set up server IP and Port #.
_server.Ip = "127.0.0.1";
_server.Port = 5001;
//Setup events for success/error checking
_server.NewConnection += new NetLib.Server.NetworkStateEventHandler(_server_NewConnection);
_server.Started += new NetLib.Server.ServerEventHandler(_server_Started);
_server.Initialized += new NetLib.Server.ServerEventHandler(_server_Initialized);
_server.BindFailure += new NetLib.Server.ServerEventHandler(_server_BindFailure);
_server.BindSuccess += new NetLib.Server.ServerEventHandler(_server_BindSuccess);
//Initialize Server and add neccesary commands
_server.Initialize();
//Clients will call sendmessage on the server,
//and the server will send the message to the neccesary clients.
_server.MessageEncoder.FriendlyCommands.Add("sendmessage", SendMessage);
}
public void SendMessage(short sender, short[] recipients, string text)
{
_server.MessagePump.Enqueue(new Packet(-1, recipients, "receivemessage", text));
}
void _server_BindSuccess()
{
//Log Bind Success at DateTime.Now
}
void _server_BindFailure()
{
//Log Bind Failure at DateTime.Now
}
void _server_Initialized()
{
//Log Initialized at DateTime.Now
}
void _server_Started()
{
//Log Started at DateTime.Now
}
void _server_NewConnection(NetLib.ServerNetworkState ns)
{
//Log New Connection with ns.Ip at DateTime.Now
BeginInvoke(new ClientConnection(AddClientToControl), ns);
}
private void AddClientToControl(NetLib.ServerNetworkState ns)
{
listBox1.Items.Add("ID: " + ns.Id + " IP: " + ns.Ip);
}
private void startServer_Click(object sender, EventArgs e)
{
_server.Start();
}
private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
{
_server.Dispose();
}
}
}
Lately, when using my TCP/IP Server and Client, I has noticed that they both stay open in Processes after I close them.
I fixed the Client staying open by disposing the socket to the server, which closes down all asynchronous threads in the background of my framework.
However, when trying to do so with the server, no matter what I do, the process stays open.
ASSUMING that the TCP/IP server is handling it's own stuff, is there ANY thing else in this code that would keep the process open?
EDIT: UPDATE:
If I put a breakpoint after the Application.Run(new ServerForm()) line in Program.cs, it will break once I press exit or call Application.Exit().
I don't know what it is hanging up the program but it's not exiting the Main.
namespace ChatServer
{
public partial class ServerForm : Form
{
private NetLib.Server _server = new NetLib.Server();
public delegate void ClientConnection(ServerNetworkState ns);
public ServerForm()
{
InitializeComponent();
}
private void ServerForm_Load(object sender, EventArgs e)
{
//Set up server IP and Port #.
_server.Ip = "127.0.0.1";
_server.Port = 5001;
//Setup events for success/error checking
_server.NewConnection += new NetLib.Server.NetworkStateEventHandler(_server_NewConnection);
_server.Started += new NetLib.Server.ServerEventHandler(_server_Started);
_server.Initialized += new NetLib.Server.ServerEventHandler(_server_Initialized);
_server.BindFailure += new NetLib.Server.ServerEventHandler(_server_BindFailure);
_server.BindSuccess += new NetLib.Server.ServerEventHandler(_server_BindSuccess);
//Initialize Server and add neccesary commands
_server.Initialize();
//Clients will call sendmessage on the server,
//and the server will send the message to the neccesary clients.
_server.MessageEncoder.FriendlyCommands.Add("sendmessage", SendMessage);
}
public void SendMessage(short sender, short[] recipients, string text)
{
_server.MessagePump.Enqueue(new Packet(-1, recipients, "receivemessage", text));
}
void _server_BindSuccess()
{
//Log Bind Success at DateTime.Now
}
void _server_BindFailure()
{
//Log Bind Failure at DateTime.Now
}
void _server_Initialized()
{
//Log Initialized at DateTime.Now
}
void _server_Started()
{
//Log Started at DateTime.Now
}
void _server_NewConnection(NetLib.ServerNetworkState ns)
{
//Log New Connection with ns.Ip at DateTime.Now
BeginInvoke(new ClientConnection(AddClientToControl), ns);
}
private void AddClientToControl(NetLib.ServerNetworkState ns)
{
listBox1.Items.Add("ID: " + ns.Id + " IP: " + ns.Ip);
}
private void startServer_Click(object sender, EventArgs e)
{
_server.Start();
}
private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
{
_server.Dispose();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NetLib.Server.Dispose() 是否处理所有“关闭”操作?在 .net 框架中,.Close() 通常会调用 dispose,但 .Dispose() 不会调用关闭操作。
Does NetLib.Server.Dispose() handle all of the 'shutdown' operations? Often times in the .net framework the .Close() calls the dispose but the .Dispose() does not call the close operations.
事实证明,图书馆中有一个线程被忽视了。感谢大家尝试找出是否有上述原因会导致问题。
回答这个问题:上面的代码中没有任何内容导致所述问题。这是一个在 Netlib 库中打开的线程,在处理和关闭任何打开的连接时从未处理过。
Turns out that there was one thread that was overlooked in the library. Thank you all for trying to trying to find out if there was anything above that would cause the problem.
To answer the question: Nothing in the code above was causing the issue stated. It was a thread that was opened in the Netlib library and was never handled when disposing and closing any connection that was open.