如何使用Rx SubscribeOn同步其他线程的结果

时间:2016-06-04 18:03:09

标签: c# multithreading system.reactive

使用Rx,我有一个代码:

class Program
{
    static void Main(string[] args)
    {
        var test = new TestClass();

        var observable = Observable
            .FromEventPattern<EventArgs>(e => test.TestEvent += e, e => test.TestEvent -= e);
            //.SubscribeOn ...
            //.Subscribe ...

        test.InvokeFromOtherThread();

        var result = observable.Wait();
    }
}


class TestClass
{
    public event EventHandler<EventArgs> TestEvent;

    public void InvokeFromOtherThread()
    {
        var task = new Task(() =>
        {
            Thread.Sleep(5000);

            TestEvent.Invoke(this, new EventArgs());
        });

        task.Start();
    }
}

我想等待在其他线程中传递的EventArgs实例。我应该如何使用SubscrinbeOn?如果我使用SubscribeOn(Scheduler.Current)没有任何反应。

0 个答案:

没有答案