system.reactive

Reactive Extensions Subscribe calling await

為{幸葍}努か 提交于 2019-12-05 13:44:40
问题 I want to perform an async call based for each event raised by a Reactive Extensions Observable. I'm also trying to keep everything synchronized as I want the async call to finish before the next event is handled. How would one go about doing something similar to the following? I say similar as the code below does not compile. settingsChangedInMemory .Subscribe(async _ => { var settings = Extract(); await SaveSettings(settings); }); I'm not sure if it changes anything, but I would need to

Rx Disposing a subscription

安稳与你 提交于 2019-12-05 13:15:19
What is the recommended way to dispose of subscriptions that are created in a loop? In the following contrived example I'm generating subscriptions in a for loop and adding them to a List and disposing them explicity by for eaching over the List This seems a bit smelly to me and I'm thinking that there has to be a cleaner way to clean up the subscriptions unless the GC disposes them when it runs. Do I need to explicity Dispose the subscriptions? class Program { static void Main(string[] args) { Func<int, IEnumerable<int>> func = x => { return Enumerable.Range(0, x); }; List<IDisposable>

Reactive Extensions swallows exceptions from OnNext() called on a thread pool thread?

 ̄綄美尐妖づ 提交于 2019-12-05 12:38:23
I use Rx 2 in .Net 4.5. When the following code runs, it just exits silently without executing the OnCompleted delegate or showing any errors. If I use Scheduler.CurrentThread in ToObservable , it will at least throw the error and terminate the program, at which point not executing OnCompleted makes sense. But when this is executed in a thread other than the main one, this behavior seems unreasonable and unacceptable. Do I miss anything? static void Main() { Enumerable.Range(0, 1) .ToObservable(Scheduler.Default) .Subscribe(o => { throw new Exception("blah"); }, () => Console.WriteLine(

Can Observable.Timer() lead to memory leaks?

放肆的年华 提交于 2019-12-05 11:54:58
Recently I noticed a small bug in my code which uses Reactive Extensions. I was subscribing to Timer but I never disposed my subscription. This resulted in a memory leak. I created snippet which highlights this danger: while (true) { Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(1)).Subscribe(Console.WriteLine); } Is this normal behaviour? Shouldn't scheduler hold weak reference to timer to get it garbage collected when subscribers lost connection with the rest of the app? This is normal, and is a feature . The semantics for Subscribe() are listen forever, or until Disposed() or

Async Disposable.Create

半城伤御伤魂 提交于 2019-12-05 10:33:03
Disposable.Create require an Action as parameter. The Action is run when the Rx subscription is being disposed. When disposing a Rx subscription I’d like to run some asynchronous clean up code, however using async () => with Action is identical to async void , which I’d like to avoid. For more details on why I want to avoid this, see here . Is it possible to create something like a Disposable.AsyncCreate , which accepts Func<Task> rather than Action . If so how should I use it as part of a CompositeDisposable ? Or are there other patterns for dealing with asynchronous Disposal? You could do

How to window/buffer IObservable<T> into chunks based on a Func<T>

非 Y 不嫁゛ 提交于 2019-12-05 10:23:48
Given a class: class Foo { DateTime Timestamp {get; set;} } ...and an IObservable<Foo> , with guaranteed monotonically increasing Timestamp s, how can I generate an IObservable<IList<Foo>> chunked into Lists based on those Timestamp s? I.e. each IList<Foo> should have five seconds of events, or whatever. I know I can use Buffer with a TimeSpan overload, but I need to take the time from the events themselves, not the wall clock. (Unless there a clever way of providing an IScheduler here which uses the IObservable itself as the source of .Now ?) If I try to use the Observable.Buffer(this

Custom Rx operator for throttling only when there's a been a recent value

筅森魡賤 提交于 2019-12-05 09:47:53
I'm trying to create an Rx operator that seems pretty useful, but I've suprisingly not found any questions on Stackoverflow that match precisely. I'd like to create a variation on Throttle that lets values through immediately if there's been a period of inactivity. My imagined use case is something like this: I have a dropdown that kicks off a web request when the value is changed. If the user holds down the arrow key and cycles rapidly through the values, I don't want to kick off a request for each value. But if I throttle the stream then the user has to wait out the throttle duration every

Reactive Extensions Parallel processing based on specific number

只愿长相守 提交于 2019-12-05 09:25:38
问题 I'm new to Reactive Extensions. I have objects collection and call a method for each object and method returns Boolean. Instead of looping through each by using for each loop and calling the method, is there a way in reactive extensions to call concurrently(fork and join) the method for a given number of objects(ex 5 at a time) and after first one done, 6th one should call method and it should continue until all the objects are done. I appreciate your response. 回答1: IObservable<bool>

IObservable ambiguous reference error

99封情书 提交于 2019-12-05 06:43:47
I'm using Reactive extensions in my WPF application. And while using it I'm getting below ambiguous reference error. The type 'System.IObservable<T>' exists in both 'mscorlib.dll' and 'System.Reactive.dll' I tried with fully qualified name also and tried this url as well, but didn't get any luck. I'm using .NET 4.0 version of Reactive Extensions. My Code: using System; // mscorlib.dll using Rx = System.Reactive; public Rx.IObservable<int> BytesReceived { get { return _bytesReceivedSubj; } } // Not valid as IObservable is in System namespace of System.Reactive. public IObservable<int>

How to use Reactive Extensions to parse a stream of characters from a serial port?

早过忘川 提交于 2019-12-05 05:43:08
I need to parse a stream of serial data coming from a test instrument, and this seems to be an excellent application for Reactive Extensions. The protocol is very simple...each "packet" is a single letter followed by numeric digits. The number of numeric digits is fixed for each packet type, but can vary between packet types. e.g. ...A1234B123456C12... I am trying to break this up into an Observable of Strings, e.g. "A1234" "B123456" "C12" ... Thought this would be simple, but don't see the obvious way to approach this (I have some experience with LINQ, but am new to Rx). Here's the code I