Is there a better way in Rx?
-
Back in Feb. 2016 I posted a How create a parallel task to call the same method -- Solution 2[^] I'm not fishing for Reputation Points, but since I'm relatively new with Rx, I'm wondering if there's a better way to do this using Rx. Since the question didn't originally reference/tag Rx, it might not have attracted the notice of the Rx folks. Nor is there any other way to tag the solution with Rx so you other Rx users would see (and comment on) it. ;) If this is inappropriate, please let me know and I'll remove it.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton
-
Back in Feb. 2016 I posted a How create a parallel task to call the same method -- Solution 2[^] I'm not fishing for Reputation Points, but since I'm relatively new with Rx, I'm wondering if there's a better way to do this using Rx. Since the question didn't originally reference/tag Rx, it might not have attracted the notice of the Rx folks. Nor is there any other way to tag the solution with Rx so you other Rx users would see (and comment on) it. ;) If this is inappropriate, please let me know and I'll remove it.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton
I assume you have seen this: Reactive Extensions (Rx) – Part 6 – Task ToObservable - Muhammad Rehan Saeed[^]
-
Back in Feb. 2016 I posted a How create a parallel task to call the same method -- Solution 2[^] I'm not fishing for Reputation Points, but since I'm relatively new with Rx, I'm wondering if there's a better way to do this using Rx. Since the question didn't originally reference/tag Rx, it might not have attracted the notice of the Rx folks. Nor is there any other way to tag the solution with Rx so you other Rx users would see (and comment on) it. ;) If this is inappropriate, please let me know and I'll remove it.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton
Hmm, your question seems more and more important the deeper I look. The previous post is ok, it works and shows some nice things. But I read this post here: StartNew is Dangerous[^] And realized that I could probably do it a bit simpler with this call instead exchanging the default values at demand of course:
Observable.Range(0, 10).
Select(e => (Task.Factory.StartNew(() => DoWork(e),
CancellationToken.None,
TaskCreationOptions.DenyChildAttach,
TaskScheduler.Default)))
.ObserveOnDispatcher()
.Subscribe(evt =>
{
//Do something with evt.Result
});But supposed I only wanted 5 parallel processes to run at the same time? How could I do that? I also seem to get the values once everyone is finished, and I'm currently not entirely sure what that is the case either. I could also use the
Observable.Start(()=>DoWork(e))
but the result in the subscriber seemed unusable to me, perhaps I mistook something here? -
Hmm, your question seems more and more important the deeper I look. The previous post is ok, it works and shows some nice things. But I read this post here: StartNew is Dangerous[^] And realized that I could probably do it a bit simpler with this call instead exchanging the default values at demand of course:
Observable.Range(0, 10).
Select(e => (Task.Factory.StartNew(() => DoWork(e),
CancellationToken.None,
TaskCreationOptions.DenyChildAttach,
TaskScheduler.Default)))
.ObserveOnDispatcher()
.Subscribe(evt =>
{
//Do something with evt.Result
});But supposed I only wanted 5 parallel processes to run at the same time? How could I do that? I also seem to get the values once everyone is finished, and I'm currently not entirely sure what that is the case either. I could also use the
Observable.Start(()=>DoWork(e))
but the result in the subscriber seemed unusable to me, perhaps I mistook something here?Your comments don't seem to be related to my code. How did you intend for them to apply? Why did you refer to
StartNew
? I didn't use that. I usedTask.Run()
. In the original question the point was to cancel any tasks once any one had returned true. My code accomplishes that. I didn't use the.ToObservable()
(that you referenced in your previous comment) on theTask
s since, in the current Rx,Task
andTask<T>
can be used whereIObservable<Unit>
andIObservable<T>
can. I don't know about how you could limit to 5 parallelTask
s as you asked..."Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton
-
Hmm, your question seems more and more important the deeper I look. The previous post is ok, it works and shows some nice things. But I read this post here: StartNew is Dangerous[^] And realized that I could probably do it a bit simpler with this call instead exchanging the default values at demand of course:
Observable.Range(0, 10).
Select(e => (Task.Factory.StartNew(() => DoWork(e),
CancellationToken.None,
TaskCreationOptions.DenyChildAttach,
TaskScheduler.Default)))
.ObserveOnDispatcher()
.Subscribe(evt =>
{
//Do something with evt.Result
});But supposed I only wanted 5 parallel processes to run at the same time? How could I do that? I also seem to get the values once everyone is finished, and I'm currently not entirely sure what that is the case either. I could also use the
Observable.Start(()=>DoWork(e))
but the result in the subscriber seemed unusable to me, perhaps I mistook something here?I just saw your Rx post on updating the UI, etc. and realized that you may have been using this thread to "prompt" me to look at that one...
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton
-
Your comments don't seem to be related to my code. How did you intend for them to apply? Why did you refer to
StartNew
? I didn't use that. I usedTask.Run()
. In the original question the point was to cancel any tasks once any one had returned true. My code accomplishes that. I didn't use the.ToObservable()
(that you referenced in your previous comment) on theTask
s since, in the current Rx,Task
andTask<T>
can be used whereIObservable<Unit>
andIObservable<T>
can. I don't know about how you could limit to 5 parallelTask
s as you asked..."Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton
I misread the question a little, I thought he also wanted to stop the calculation with a UI event also. I saw this question mainly as a cross thread communication issue. The reason I liked the
.ToObservable()
is that I could easily move the thread initialization off the UI by calling.SubscribeOn(Scheduler.Default)
, I always like to do that as soon as possible. But I guess it's not going to make a big deal of difference. As for my question, I realized that restricting the number of new threads is not necessary, as theRun.Task
orTaskFactory.StartNew
manages that for me (They could work in the same way with proper initialization). I though the methods always started a new thread, but that is not the case. Guess I need to drink a little more :java: Sorry for my confusion here :) -
I misread the question a little, I thought he also wanted to stop the calculation with a UI event also. I saw this question mainly as a cross thread communication issue. The reason I liked the
.ToObservable()
is that I could easily move the thread initialization off the UI by calling.SubscribeOn(Scheduler.Default)
, I always like to do that as soon as possible. But I guess it's not going to make a big deal of difference. As for my question, I realized that restricting the number of new threads is not necessary, as theRun.Task
orTaskFactory.StartNew
manages that for me (They could work in the same way with proper initialization). I though the methods always started a new thread, but that is not the case. Guess I need to drink a little more :java: Sorry for my confusion here :)Since my solution used a
CancellationToken
the UI should be able to use that to stop all of the calculations that way also. TheCancellationTokenSource
would need to be "exposed", or a method to.Cancel()
the token source would need to be provided."Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton