system.reactive

How do I share an observable with publish and connect?

点点圈 提交于 2019-12-08 22:31:16
问题 I have an observable data stream that I am applying operations to, splitting into two separate streams, applying more (distinct) operations to each of the two streams, and merging together again. I am trying to share the observable between two subscribers using Publish and Connect but each of the subscribers seems to be using a separate stream. That is, in the example below, I see "Doing an expensive operation" printed once for each item in the stream for both of the subscribers . (Imagine

ObserveOn(Scheduler.CurrentThread) doesn't cause subscribed Action to be run on original thread

末鹿安然 提交于 2019-12-08 16:59:33
I have an Action that takes a callback it will call with a generic argument once it completes, i.e. an Action<Action<T>> . I want to show a busy spinner when the action starts and then take it away when it calls the callback so I created a simple utility to do that. The problem I'm having is that the user will expect their callback to run on the original calling thread but it does not always do so. It almost always works perfectly in unit tests (nUnit) but doesn't work for some calls when the application is actually running (WPF, .Net 4). Here are the relevant bits of what I have void

Rx How to parallelize long-running task?

邮差的信 提交于 2019-12-08 10:45:38
问题 I have the following snippet that enumerates elements of some xml (read from the output of a svn log --xml ... process) then runs a long-running method for each xml element. var proc = Process.Start(svnProcInfo); var xml = XDocument.Load(proc.StandardOutput); var xElements = xml.Descendants("path") .ToObservable() //.SubscribeOn(ThreadPoolScheduler.Instance) .Select(descendant => return LongRunning(descendant)); xElements //.SubscribeOn(NewThreadScheduler.Default) .Subscribe(result => Console

What Am I Missing About Reactive Extension Timers?

天涯浪子 提交于 2019-12-08 10:04:48
问题 I have this: watchers .ToObservable() // needs to be observable .SelectMany(watcher => // working on each watcher Observable // create a timer for the watcher .Timer(watcher.StartTime, TimeSpan.FromHours(watcher.Interval)) .SelectMany(Observable.FromAsync( async () => new { watcher, result = await CheckFolder(watcher.Path) }))) .Subscribe(x => Console.WriteLine(string.Format("Watcher: {0}\tResult: {1}\tTime: {2}", x.watcher.Name, x.result, DateTimeOffset.Now))); // tell everyone what happened

How to convert img url to BASE64 string in HTML on one method chain by using LINQ or Rx

a 夏天 提交于 2019-12-08 08:57:14
问题 I found I could generate XDocument object from html by using SgmlReader.SL. https://bitbucket.org/neuecc/sgmlreader.sl/ The code is like this. public XDocument Html(TextReader reader) { XDocument xml; using (var sgmlReader = new SgmlReader { DocType = "HTML", CaseFolding = CaseFolding.ToLower, InputStream = reader }) { xml = XDocument.Load(sgmlReader); } return xml; } Also we can get src attributes of img tags from the XDocument object. var ns = xml.Root.Name.Namespace; var imgQuery = xml

Display progress while restoring database in linqpad with reative extensions

泄露秘密 提交于 2019-12-08 07:26:55
问题 I have the following C# code. var databaseRestore = new Microsoft.SqlServer.Management.Smo.Restore(); //databaseRestore.PercentComplete += CompletionStatusInPercent; //databaseRestore.PercentCompleteNotification = 10; //databaseRestore.Complete += Restore_Completed; ... var complete = Observable .FromEventPattern(databaseRestore, "Complete") .Select(x=>x.EventArgs as ServerMessageEventArgs) .Select(x=>x.Error.Message) .Take(1) .DumpLive("Complete"); var percentComplete = Observable

How to use virtual time for Task.Run when testing Observables with Reactive Extensions

限于喜欢 提交于 2019-12-08 06:52:59
问题 I have the following function I wish to test /// Items are processed asynchronously via fn as they arrive. However /// if an item arrives before the last asynchronous operation has /// completed then the cancellation token passed to fn will be /// triggered enabling the task to be canceled in a best effort /// way. public static IObservable<U> SelectWithCancellation<T, U> ( this IObservable<T> This , Func<CancellationToken, T, Task<U>> fn ) { return This .Select(v=>Observable.FromAsync(token=

ObserveOn(Scheduler.CurrentThread) doesn't cause subscribed Action to be run on original thread

。_饼干妹妹 提交于 2019-12-08 06:51:03
问题 I have an Action that takes a callback it will call with a generic argument once it completes, i.e. an Action<Action<T>> . I want to show a busy spinner when the action starts and then take it away when it calls the callback so I created a simple utility to do that. The problem I'm having is that the user will expect their callback to run on the original calling thread but it does not always do so. It almost always works perfectly in unit tests (nUnit) but doesn't work for some calls when the

Observable.Retry doesn't work as expected

南笙酒味 提交于 2019-12-08 06:35:56
问题 I have a sequence of numbers that are processed using an async method. I'm simulating a remote service call that may fail . In case of failure, I would like to retry until the call successes. The problem is that with the code I'm trying, every time an exception is thrown in the async method, the sequence seems to hang forever . You can test it with this simple code snippet (it's tested in LINQPad) Random rnd = new Random(); void Main() { var numbers = Enumerable.Range(1, 10).ToObservable();

Parallel Cache using Reactive extensions

会有一股神秘感。 提交于 2019-12-08 05:45:33
问题 I'm looking to build a parallel cache. The requirement is that there will be n number of datacollectors that need to be fired at once. Each of these data collectors will hit a boundary layer (call this the service layer) and retrieve data. However, since this is within the same request (WCF), if 2 data collectors need to invoke the same method on the service layer, I don't want the second request to wait for the first one to complete. This needs to be build transparently to developers