【问题标题】:Why doesn't my asynchronous write function run?为什么我的异步写入函数不运行?
【发布时间】:2020-06-18 22:09:19
【问题描述】:

好的,所以我一直在制作一个异步 IRC,它工作正常,除了一个问题。客户端可以写,服务器可以读。除了服务器不能写,客户端不能读。有什么帮助吗?

https://github.com/theprogrammer4568/IRC

        async public static Task Receive()
        {
            while(true)
            {
                Byte[] bytes = new Byte[256];

                int data = await Server.stream.ReadAsync(bytes, 0, bytes.Length);
                string message = System.Text.Encoding.ASCII.GetString(bytes, 0, data);

                Console.WriteLine(message);
            }
        }


        async public static Task Send()
        {
            while(true)
            {
                Byte[] bytes = new Byte[256];

                string message = Console.ReadLine();
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

                await Server.stream.WriteAsync(data, 0, data.Length);
            }
        }

        async static Task Async()
        {
            await Task.Run(Read.Receive);
            await Task.Run(Write.Send);
        }

【问题讨论】:

标签: c# asynchronous irc


【解决方案1】:

你的 Send 函数永远不会运行,因为你的 Receive 函数永远循环。

即使您使用Task.Run() 在不同的线程上运行它们,在运行Send 之前,您仍然是await 处理Receive 的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 2020-11-09
    • 2023-01-23
    • 2015-07-05
    • 2015-02-27
    • 2018-05-02
    • 2023-03-12
    相关资源
    最近更新 更多