简体   繁体   中英

C# Program keeps running in Processes after closing form

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();
    }
}

}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM