的zeromq的pub / sub用C#的winform的例子 [英] examples of zeromq pub/sub with C# winform
问题描述
我试图创建一个使用ZeroMQ(clrzmq .NET绑定(86)通过的NuGet)在pub / sub模型一个C#WinForm应用程序。
I'm trying to create a C# Winform application that uses ZeroMQ (clrzmq .net bindings (x86) via nuget) in a pub/sub model.
一番搜索之后,我只能找到独立的C#示例,其中的code使用while语句无限期地处理新邮件。当我尝试使用这些例子,我不知道放在哪里code,而且只有几个街区的GUI和一切。
After much searching, I can only find standalone C# examples where the code uses a while statement to process new messages indefinitely. When I try to use these examples, I don't know where to put the code, and it just blocks the gui and everything else.
我不知道这是不可能做到不使用另一个线程,但我是在IM pression了ZeroMQ的异步行为可以工作,没有编码的额外的线程。也许我只是不知道放在哪里zeromq code,也许我真的需要另一个线程。
I don't know if it's impossible to do without using another thread, but I was under the impression that ZeroMQ's asynchronous behaviors could work without coding extra threads. Perhaps I just don't know where to put the zeromq code, or perhaps I really do need another thread.
如果有人可以提供一个简单的pub / sub例如用在何处实际插入code到一个默认的C#的winform应用方向将是非常美联社preciated。
If someone could provide a simple pub/sub example with directions of where to actually insert the code into a default C# winform application it would be very appreciated.
推荐答案
我使用的是假设 clrzmq ZeroMq包装在您的项目。据我知道这是不可能收到消息无阻塞一个简单的循环使用clrzmq,它会阻止无限期,为特定的时间量(通过提供超时到接收方式),或者直到你收到一条消息
I am assuming you are using the clrzmq ZeroMq wrapper in your project. As far as I know it is not possible to receive message non-blocking in a simple loop using clrzmq, it will block either indefinitely, for a specific amount of time (by supplying a timeout to the receive method) or until you receive a message.
然而,这是相当琐碎成立一个线程来定期轮询插座,推动传入邮件到队列
。然后,您可以使用例如一个简单的WinForms 定时器
定期从(共享)队列出队的任何未决消息
。这里是一个线程用户的工作例如:
However, it is fairly trivial to set up a thread to poll the socket periodically and push incoming messages to onto a Queue
. You may then use for example a simple WinForms Timer
to periodically dequeue any pending messages from that (shared) Queue
. Here is a working example of a threaded subscriber:
public class ZeroMqSubscriber
{
private readonly ZmqContext _zmqContext;
private readonly ZmqSocket _zmqSocket;
private readonly Thread _workerThread;
private readonly ManualResetEvent _stopEvent = new ManualResetEvent(false);
private readonly object _locker = new object();
private readonly Queue<string> _queue = new Queue<string>();
public ZeroMqSubscriber(string endPoint)
{
_zmqContext = ZmqContext.Create();
_zmqSocket = _zmqContext.CreateSocket(SocketType.SUB);
_zmqSocket.Connect(endPoint);
_zmqSocket.SubscribeAll();
_workerThread = new Thread(ReceiveData);
_workerThread.Start();
}
public string[] GetMessages()
{
lock (_locker)
{
var messages = _queue.ToArray();
_queue.Clear();
return messages;
}
}
public void Stop()
{
_stopEvent.Set();
_workerThread.Join();
}
private void ReceiveData()
{
try
{
while (!_stopEvent.WaitOne(0))
{
var message = _zmqSocket.Receive(Encoding.UTF8,
new TimeSpan(0, 0, 0, 1));
if (string.IsNullOrEmpty(message))
continue;
lock (_locker)
_queue.Enqueue(message);
}
}
finally
{
_zmqSocket.Dispose();
_zmqContext.Dispose();
}
}
}
在表格
您只需定期轮询队列(本例中使用了窗体Timer
键,就追加消息数据到文本框
):
From the Form
you simply poll the Queue periodically (this example uses a Forms Timer
and simply appends the message data to a Textbox
):
private readonly ZeroMqSubscriber _zeroMqSubscriber =
new ZeroMqSubscriber("tcp://127.0.0.1:5000");
void ReceiveTimerTick(object sender, EventArgs e)
{
var messages = _zeroMqSubscriber.GetMessages();
foreach (var message in messages)
_textbox.AppendText(message + Environment.NewLine);
}
这篇关于的zeromq的pub / sub用C#的winform的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!