简体   繁体   English

关闭表单后,C#程序继续在进程中运行

[英]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. 最近,在使用我的TCP / IP服务器和客户端时,我注意到在关闭它们之后它们都在进程中保持打开状态。

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? 假设TCP / IP服务器正在处理它自己的东西,这段代码中还有什么东西可以保持进程开放吗?

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(). 编辑:更新:如果我在Program.cs中的Application.Run(new ServerForm())行之后放置一个断点,它将在我按下退出或调用Application.Exit()时中断。

I don't know what it is hanging up the program but it's not exiting the Main. 我不知道它是什么挂起了程序,但它没有退出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? NetLib.Server.Dispose()是否处理​​所有'关闭'操作? Often times in the .net framework the .Close() calls the dispose but the .Dispose() does not call the close operations. 通常在.net框架中,.Close()会调用dispose,但.Dispose()不会调用close操作。

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. 这是一个在Netlib库中打开的线程,在处理和关闭任何打开的连接时从未处理过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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