system.reactive

How to subscribe to, but buffer data from, an IObservable until another IObservable has published?

人走茶凉 提交于 2020-01-01 18:56:45
问题 I want to: Immediately subscribe to an IObservable<T> , but immediately start buffering any T that is received (i.e. not yet seen by my IObserver<T> ). Do some work. When the work is completed, flush the buffer to my IObserver<T> and continue It is quite important that the subscription is the first thing that happens. In a 'marble diagram' form I am after something like this... Time T+1 2 3 4 5 6 7 8 s1:IObservable<int> 1 2 3 4 5 6 7 8 s2:IObservable<bool> t r: IObservable<int> 1 3 4 5 6 7 8

Rx - How to create IObservable<T> from Task<T> such that unsubscribing cancels the task?

馋奶兔 提交于 2020-01-01 17:14:08
问题 I'm new to Rx so bear with me. I want to wrap a Task<T> in an IObservable<T> . So far so good: Task<T> task = Task.Factory.StartNew(...); IObservable<T> obs = task.ToObservable(); Now, what I want is to signal the task to cancel when the observer unsubscribes: var cancel = new CancellationToken(); Task<T> task = Task.Factory.StartNew(..., cancel); IObservable<T> obs = task.ToObservable(); //there should be a way to tie the cancel token //to the IObservable (?) IDisposable disposable = obs

how to use yield to return the collection of Item in parallel block or Task

谁都会走 提交于 2020-01-01 10:59:06
问题 I looking for help on how to make use of yield keyword to return IEnumberable in parallel blocks or Task block. Below is the pseudo code public IEnumerable<List<T>> ReadFile( ) { foreach (string filepath in lstOfFiles) { var stream = new FileStream(filepath , FileMode.Open, FileAccess.Read); foreach (var item in ReadStream(stream)) yield return item; //where item is of type List<string> } } I want to convert above code to parallel block like below lstOfFiles.AsParallel() .ForAll(filepath => {

Unit testing for an event using Reactive Extensions

只谈情不闲聊 提交于 2020-01-01 10:19:06
问题 I'm using Reactive Extensions for .NET (Rx) to expose events as IObservable<T> . I want to create an unit test where I assert that a particular event is fired. Here is a simplified version of the class I want to test: public sealed class ClassUnderTest : IDisposable { Subject<Unit> subject = new Subject<Unit>(); public IObservable<Unit> SomethingHappened { get { return this.subject.AsObservable(); } } public void DoSomething() { this.subject.OnNext(new Unit()); } public void Dispose() { this

Synchronicity in RxJS

六月ゝ 毕业季﹏ 提交于 2020-01-01 10:14:44
问题 I would expect that the following code would run asynchronously: var range = Rx.Observable.range(0, 3000000); range.subscribe( function(x) {}, function(err) {}, function() { console.log('Completed'); }); console.log('Hello World'); But that's not the case. It takes a while to go through the big range of numbers and only when it is completed the execution is resumed, you can try the code here. I am confused as to when to expect RxJS to behave synchronously or asynchronously. Does it depend on

Run methods in Parallel using Reactive

时光怂恿深爱的人放手 提交于 2020-01-01 06:53:08
问题 Halo there all, I was looking for a solution with RX framework. My C# 4.0 class is going to call 2 different methods and to save the time, I want to do it in parallel. Is there any way to run 2 different methods in parallel using Reactive Framework ? Not only run those 2 methods parallel, but also one should wait for other to complete and combine the both results. Example as shown below: AccountClass ac = new AccountClass(); string val1 = ac.Method1(); bool val2 = ac.Method2(); How I can run

Reactive: Trying to understand how Subject<T> work

对着背影说爱祢 提交于 2020-01-01 06:25:11
问题 Trying to understand how the Subject<T> , ReplaySubject<T> and other work. Here is example: (Subject is Observable and observer) public IObservable<int> CreateObservable() { Subject<int> subj = new Subject<int>(); // case 1 ReplaySubject<int> subj = new ReplaySubject<int>(); // case 2 Random rnd = new Random(); int maxValue = rnd.Next(20); Trace.TraceInformation("Max value is: " + maxValue.ToString()); subj.OnNext(-1); // specific value for(int iCounter = 0; iCounter < maxValue; iCounter++) {

Async Queue Processing With Reactive Extensions

余生颓废 提交于 2020-01-01 03:04:22
问题 There are a couple of articles on this, and I have this working...but I want to know how to set a max number of Task threads for my Observable subscriptions at once. I have the following to parallelize async saving of log entries: private BlockingCollection<ILogEntry> logEntryQueue; and logEntryQueue = new BlockingCollection<ILogEntry>(); logEntryQueue.GetConsumingEnumerable().ToObservable(Scheduler.TaskPool).Subscribe(SaveLogEntry); To schedule my saving...but how do I specify the max

Is there a way to hold a collection of “messages” upto size 1 MB and write the result to JSON/CSV file

a 夏天 提交于 2019-12-31 04:33:25
问题 I have a blocking queue which keep getting messages through some app, now In asp.net app I tried to consume the queue and write the output into CSV/JSON file. Here I want to hold messages up to 1MB which receive from blocking queue and then write it out, now again hold data for 1MB and write again...so on. In below code, I'm using system.reactive buffer and can hold number of observable 's and write to JSON, but Is there any way on size of observable 's? class Program { private static

Scheduling a IEnumerable periodically with .NET reactive extensions

*爱你&永不变心* 提交于 2019-12-31 03:51:32
问题 Say for example I have an enumerable dim e = Enumerable.Range(0, 1024) I'd like to be able to do dim o = e.ToObservable(Timespan.FromSeconds(1)) So that the observable would generate values every second until the enumerable is exhausted. I can't figure a simple way to do this. 回答1: I have also looked for the solution and after reading the intro to rx made my self one: There is an Observable.Generate() overload which I have used to make my own ToObservable() extension method, taking TimeSpan