直接贴代码了:

 

using System;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace SimpleDataFlowSample
{
    class Program
    {
        static void Main()
        {
            Task t1 = Task.Run(() => Producer());
            Task t2 = Task.Run(async () => await ConsumerAsync());
            Task.WaitAll(t1, t2);
        }

        private static BufferBlock<string> s_buffer = new BufferBlock<string>();

        public static void Producer()
        {
            bool exit = false;
            while (!exit)
            {
                string input = Console.ReadLine();
                if (string.Compare(input, "exit", ignoreCase: true) == 0)
                {
                    exit = true;
                }
                else
                {
                    s_buffer.Post(input);
                }
            }
        }

        public static async Task ConsumerAsync()
        {
            while (true)
            {
                string data = await s_buffer.ReceiveAsync();
                Console.WriteLine($"user input: {data}");
            }
        }
    }
}

 

一个简单 System.Threading.Tasks.Dataflow.BufferBlock 示例

谢谢浏览!

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
  • 2022-02-28
  • 2022-02-05
  • 2022-02-27
  • 2022-12-23
猜你喜欢
  • 2021-06-22
  • 2021-10-07
  • 2021-11-15
  • 2021-05-21
  • 2021-10-21
  • 2021-07-17
  • 2022-12-23
相关资源
相似解决方案