等待 AcceptTcpClientAsync() 跳出 while 循环

awaiting AcceptTcpClientAsync() breaks out of while loop

我正在尝试构建一个简单的多客户端 TCP 服务器。我有一个主程序调用的异步函数,它运行到一个 while 循环中,等待客户端连接。我的问题是程序永远不会超过等待(在没有客户端连接的情况下是预期的)并且会自行结束。

    public async void Start()
    {
        try
        {
            Listener.Start();
            Running = true;
            while (Running)
            {
                var client = await Listener.AcceptTcpClientAsync();
                await Task.Run(() => HandleConnection(client, token));
            }
        }
        catch (SocketException)
        {
            throw;
        }
    }

没有抛出异常,调试刚刚结束。我不明白程序是如何离开无限 while 循环的。

I suspect that you don't have any other threads running any useful operations and you're letting yourself return from your Main - which means you've got no threads doing anything and the process exits. A simple fix would be to make this async Task instead and let that flow up until you become async Main or you end up actually blocking a real thread. - Damien_The_Unbeliever

这就是问题所在。