I'm trying to write a typical stock trading program, which receives stock tickers/orders/trades from netmq, turn the streams into IObservable, and show them on a WPF frontend. I try to use async/await with NetMQ blocking ReceiveString (suppose I am expecting some string input) so that the ReceiveString loop wouldn't block the main (UI) thread. As I'm still new to C#, I take the answer of Dave Sexton in this post: (https://social.msdn.microsoft.com/Forums/en-US/b0cf96b0-d23e-4461-9d2b-ca989be678dc/where-is-iasyncenumerable-in-the-lastest-release?forum=rx) and trying to write some examples like this:
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using NetMQ;
using NetMQ.Sockets;
using System.Reactive;
using System.Reactive.Linq;
namespace App1
{
class MainClass
{
// publisher for testing, should be an external data publisher in real environment
public static Thread StartPublisher(PublisherSocket s)
{
s.Bind("inproc://test");
var thr = new Thread(() => {
Console.WriteLine("Start publishing...");
while (true) {
Thread.Sleep(500);
s.Send("hello");
}
});
thr.Start();
return thr;
}
public static IObservable<string> Receive(SubscriberSocket s)
{
s.Connect("inproc://test");
s.Subscribe("");
return Observable.Create<string>(
async observer =>
{
while (true)
{
var result = await s.ReceiveString();
observer.OnNext(result);
}
});
}
public static void Main(string[] args)
{
var ctx = NetMQContext.Create();
var sub = ctx.CreateSubscriberSocket();
var pub = ctx.CreatePublisherSocket();
StartPublisher(pub);
Receive(sub).Subscribe(Console.WriteLine);
Console.ReadLine();
}
}
}
It fails to compile with "cannot await string". While I understand it might be expecting a Task, I don't quite figure out how to get the whole thing up.
To wrap again: what I'm trying to achieve is simply get IObservable streams of ticker/orders/trades from netmq using simple blocking apis, but without really blocking the main thread.
Anything I can do with it? Thanks a lot.