system.reactive

“Exclusive” and “Default” Subscription Modes in Rx

青春壹個敷衍的年華 提交于 2019-12-07 18:29:53
问题 I have an observable sequence of event objects and a number of observers handling specific types of events. I need to accomplish the following scenarios: Some event types need to be handled by the first observer matching a condition (e.g. observable.SubscribeExclusively(x=>{}) and become "unobservable" to the others. If there are no subscriptions, set some default handler (e.g. observable.SubscribeIfNoSubscriptions(x=>{})) so that no items get lost (this handler may for example save the item

Reactive Framework (RX) and dealing with events Asynchronously

我是研究僧i 提交于 2019-12-07 14:55:49
问题 So I'm just playing around with RX and learning it. I started playing with Events, and wanted to know how to subscribe to events, and process the results in batches asynchronously. Allow me to explain with code: Simple class that raises events: public class EventRaisingClass { public event EventHandler<SomeEventArgs> EventOccured; //some other code that raises event... } public class SomeEventArgs : EventArgs { public SomeEventArgs(int data) { this.SomeArg = data; } public int SomeArg { get;

Can Observable.Timer() lead to memory leaks?

此生再无相见时 提交于 2019-12-07 09:16:23
问题 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? 回答1: This is

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

纵饮孤独 提交于 2019-12-07 08:52:40
问题 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)

Rx Disposing a subscription

本秂侑毒 提交于 2019-12-07 08:42:32
问题 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)

C#5 ReadAsync and Iterators

本小妞迷上赌 提交于 2019-12-07 08:17:01
问题 I am trying to convert the below class to lazily return a file. public class ObservableFile2 : IObservable<string> { private readonly IObservable<string> subject; public ObservableFile2(string fileName) { subject = Observable.Using<string, StreamReader> ( () => new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)), streamReader => ObserveLines(streamReader) ); } private IObservable<string> ObserveLines(StreamReader streamReader) { return ReadLines

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

寵の児 提交于 2019-12-07 06:27:04
问题 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

Async Disposable.Create

老子叫甜甜 提交于 2019-12-07 05:54:00
问题 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

Update WPF progress bar while filling DataSet, all using Rx

元气小坏坏 提交于 2019-12-07 05:08:27
I'm a bit new in Rx, so please excuse me if this seems silly or obvious... I have an application which at a certain time, is meant to scan a selected folder and retrieve all files recursively, after which it needs to store them in a database. I would like to display a progress bar during that process while keeping the UI responsive of course. A cancel button would also be nice at a later stage. I've implemented this using Rx, like so: // Get a list of all the files var enumeratedFiles = Directory.EnumerateFiles(targetDirectory, "*.*", SearchOption.AllDirectories); // prepare the progress bar

How to use Rx.Nex extension ForEachAsync with async action

旧城冷巷雨未停 提交于 2019-12-07 04:50:00
问题 I have code which streams data down from SQL and writes it to a different store. The code is approximately this: using (var cmd = new SqlCommand("select * from MyTable", connection)) { using (var reader = await cmd.ExecuteReaderAsync()) { var list = new List<MyData>(); while (await reader.ReadAsync()) { var row = GetRow(reader); list.Add(row); if (list.Count == BatchSize) { await WriteDataAsync(list); list.Clear(); } } if (list.Count > 0) { await WriteDataAsync(list); } } } I would like to