简体   繁体   English

将两个事件合并为IObservable

[英]Combine two events into IObservable

I have next interface: 我有下一个界面:

public interface ITask
{
    event EventHandler<EventArgs> Completed;
    event EventHandler<UpdateEventArgs> Update;
}

I want to have IObservable, that will fire IObserver.OnNext when ITask.Update event fires and IObserver.OnCompleted when ITask.Completed event fires. 我想拥有IObservable,当ITask.Update事件触发时将触发IObserver.OnNext,而当ITask.Completed事件触发时将触发IObserver.OnCompleted。 Can I accomplish this combining using RX, or thi is impossible and I shuld make my custom implementation for this. 我可以使用RX完成此合并吗,还是这是不可能的,我应该为此进行自定义实现。

Lke this: 像这样:

/* Assume you have an instance of ITask here */
ITask task;

var updates = Observable.FromEventPattern<UpdateEventArgs>(task, "Update");
var completed = Observable.FromEventPattern<EventArgs>(task, "Completed");

var desiredStream = updates.TakeUntil(completed);

/* example usage */
desiredStream.Subscribe(Console.WriteLine,
                        () => Console.WriteLine("Done"));

Here's a contrived example, that shows 5 "updates" being published on your standard .NET events, and then an observable sequence that wraps the standard event and ends when the "completed" occurs. 这是一个人为的示例,它显示了在标准.NET事件中发布的5个“更新”,然后是一个可观察的序列,该序列包装了标准事件并在“完成”发生时结束。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class UpdateEventArgs : EventArgs
    {
        public string Value { get; set; }
    }
    public interface ITask
    {
        event EventHandler<EventArgs> Completed;
        event EventHandler<UpdateEventArgs> Update;
    }

    public partial class Form1 : Form, ITask
    {
        public event EventHandler<EventArgs> Completed;

        public event EventHandler<UpdateEventArgs> Update;

        private Timer m_timer = new Timer();

        private int m_timercount = 5;

        public Form1()
        {
            InitializeComponent();

            var obUpdate = Observable.FromEventPattern<UpdateEventArgs>(this, "Update");
            var obCompleted = Observable.FromEventPattern<EventArgs>(this, "Completed");

            var obUpdatesUntilCompletedSequence = obUpdate.TakeUntil(obCompleted);

            obUpdatesUntilCompletedSequence.Subscribe(new Action<EventPattern<UpdateEventArgs>>(UpdateOccurred), new Action(UpdateCompleted));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_timer.Interval = 1000;
            m_timer.Start();
            m_timer.Tick += m_timer_Tick;
        }

        void m_timer_Tick(object sender, EventArgs e)
        {
            Update(this, new UpdateEventArgs { Value = DateTime.Now.ToString() });

            if (--m_timercount == 0)
            {
                Completed(this, new EventArgs());

                m_timer.Stop();
            }
        }

        private void UpdateOccurred(EventPattern<UpdateEventArgs> update)
        {
            System.Diagnostics.Debug.WriteLine(update.EventArgs.Value);
        }

        private void UpdateCompleted()
        {
            System.Diagnostics.Debug.WriteLine("No more updated will be received");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用IObservable而不是事件 - Use of IObservable instead of events 将一个 IObservable&lt;(long,float)&gt; 拆分为两个 IObservable 的 observable<long> 和 IObservable<float> - Split a IObservable<(long,float)> into two observables of IObservable<long> and IObservable<float> 具有嵌套事件的IObservable的执行上下文 - Execution Context on IObservable with nested Events 如何获取IObservable的最新更改事件 <IObservable<T> &gt;? - How to get latest changed events of IObservable<IObservable<T>>? 使用Rx将多个事件源合并到一个IObservable中 - Combine multiple event sources into one IObservable with Rx 如何结合两个热IObservable <int> -s所以结果给出了一个减去其他总和的总和? - How to combine two hot IObservable<int>-s so the resulting one gives back sum of one minus sum of others? Rx IObservable缓冲以平滑突发事件 - Rx IObservable buffering to smooth out bursts of events 如何结合“IObservable<bool> 一个订阅中的 IsActive”和“bool IsEnabled” - How to combine "IObservable<bool> IsActive" and "bool IsEnabled" in one subsciption 如何在 UniRx/Rx.NET 中组合 IObservable 序列? - How to combine IObservable sequences in UniRx/Rx.NET? 在我的C#应用​​程序中结合使用Task和IObservable是不好的做法? - Is it a bad practice to combine use of Task and IObservable in my C# application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM