简体   繁体   中英

Best Way to send message to thread

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 in Blocking Queues & Thread's Communication in C# )

  2. Running a message loop Using Application.Run of systems winforms in the thread context and before running the application.run subscribe to an event that capture the messsage in the thread context.

  3. Using socket or named pipe to send the thread a message.

In Linux I am used to do this with unix domain sockets, what is the equivalent way to do it in windows? share memory file? named pipe? What do you think?

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<T> wrapped round a ConcurrentQueue<T> 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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