Best Way to send message to thread

后端 未结 1 1497
攒了一身酷
攒了一身酷 2021-01-01 18:28

What is the most efficient and fastest way to send message to a thread (not process) that run in while(1) loop in c#/.net:

  1. Using a synchronized queue (such

1条回答
  •  迷失自我
    2021-01-01 19:14

    I'd use a producer/consumer queue, personally. That's effectively what the WinForms message loop is, just in a Windows Forms-specific way.

    Note that if you're able to use .NET 4.0, there are collections built into the framework which make this very easy. In particular, using a BlockingCollection wrapped round a ConcurrentQueue will do what you want.

    I wouldn't personally use the GeeksCafe code - I'd encapsulate the producer/consumer nature into its own class which wraps a queue, rather than treating any queue in that way via extension methods. In particular, you need all parties to handle the queue correctly, which means it's better to give it its own API in my view.

    0 讨论(0)
提交回复
热议问题