Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is there a better way in Rx?

Is there a better way in Rx?

Scheduled Pinned Locked Moved C#
questioncom
7 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt T Heffron
    wrote on last edited by
    #1

    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

    K 2 Replies Last reply
    0
    • M Matt T Heffron

      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

      K Offline
      K Offline
      Kenneth Haugland
      wrote on last edited by
      #2

      I assume you have seen this: Reactive Extensions (Rx) – Part 6 – Task ToObservable - Muhammad Rehan Saeed[^]

      1 Reply Last reply
      0
      • M Matt T Heffron

        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

        K Offline
        K Offline
        Kenneth Haugland
        wrote on last edited by
        #3

        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?

        M 2 Replies Last reply
        0
        • K Kenneth Haugland

          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?

          M Offline
          M Offline
          Matt T Heffron
          wrote on last edited by
          #4

          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 used Task.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 the Tasks since, in the current Rx, Task and Task<T> can be used where IObservable<Unit> and IObservable<T> can. I don't know about how you could limit to 5 parallel Tasks 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

          K 1 Reply Last reply
          0
          • K Kenneth Haugland

            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?

            M Offline
            M Offline
            Matt T Heffron
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • M Matt T Heffron

              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 used Task.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 the Tasks since, in the current Rx, Task and Task<T> can be used where IObservable<Unit> and IObservable<T> can. I don't know about how you could limit to 5 parallel Tasks 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

              K Offline
              K Offline
              Kenneth Haugland
              wrote on last edited by
              #6

              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 the Run.Task or TaskFactory.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 :)

              M 1 Reply Last reply
              0
              • K Kenneth Haugland

                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 the Run.Task or TaskFactory.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 :)

                M Offline
                M Offline
                Matt T Heffron
                wrote on last edited by
                #7

                Since my solution used a CancellationToken the UI should be able to use that to stop all of the calculations that way also. The CancellationTokenSource 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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups