C # program continues to work in processes after closing a form

Recently, when I use my TCP / IP server and client, I noticed that both of them remain open in Processes after they are closed.

I found that the Client remains open by deleting the socket on the server, which closes all asynchronous threads in the background of my structure.

However, when I try to do this with the server, no matter what I do, the process remains open.

ACCEPTING that the TCP / IP server processes its own things, is there anything else in this code to keep the process open?

EDIT: UPDATE: If I put a breakpoint after the line Application.Run (new ServerForm ()) in Program.cs, it will break as soon as I click on exit or call Application.Exit ().

I do not know that this terminates the program, but does not exit 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(); } } 

}

+4
source share
2 answers

It turns out that one stream was skipped in the library. Thanks to everyone for trying to find out if there is anything above that might cause the problem.

To answer the question: nothing in the code above caused a problem. It was a thread that was opened in the Netlib library and was never processed when deleting and closing any open connection.

0
source

Does NetLib.Server.Dispose () perform all shutdown operations? Often in a .net structure, a call to .Close () calls dispose, but .Dispose () does not call a close operation.

0
source

Source: https://habr.com/ru/post/1388664/


All Articles