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. Task.Run.Wait

Task.Run.Wait

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 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.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    While I was reading code produced by coworker of mine, I got confused. What does

    public void DoSomething(double someParameter)
    {
    _SomeComponent.Set(someParameter);
    Task.Run(() => Check(someParameter)).Wait();
    }

    actually do, i.e. how does it differ from

    public void DoSomething(double someParameter)
    {
    _SomeComponent.Set(someParameter);
    Check(someParameter);
    }

    The Check function is synchronous. So, the two versions could be equivalent? That Task.Run starts the Check function in a new Task. But because of the Wait, DoSomething won't be left immediately after starting that Task, but only when that Task ran to completion (or failure). But I am too confused by that snippet now to be sure of my assumptions.

    Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

    OriginalGriffO J 2 Replies Last reply
    0
    • B Bernhard Hiller

      While I was reading code produced by coworker of mine, I got confused. What does

      public void DoSomething(double someParameter)
      {
      _SomeComponent.Set(someParameter);
      Task.Run(() => Check(someParameter)).Wait();
      }

      actually do, i.e. how does it differ from

      public void DoSomething(double someParameter)
      {
      _SomeComponent.Set(someParameter);
      Check(someParameter);
      }

      The Check function is synchronous. So, the two versions could be equivalent? That Task.Run starts the Check function in a new Task. But because of the Wait, DoSomething won't be left immediately after starting that Task, but only when that Task ran to completion (or failure). But I am too confused by that snippet now to be sure of my assumptions.

      Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Bernhard Hiller wrote:

      how does it differ from

      Well, it uses more memory, and adds a thread to the system ... But no, you are right. In essence starting a Task to call Check and then calling Wait is the same as calling the method directly: the calling method will not continue until Check is complete. It's possible that the cow-orker doesn't know what he is doing, it's possible that the Check method was used elsewhere and called via a Task without the Wait, but that code caused problems when re-used. We don't know, and will probably never find out. But it's pretty poor code in it's current form!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Bernhard Hiller wrote:

        how does it differ from

        Well, it uses more memory, and adds a thread to the system ... But no, you are right. In essence starting a Task to call Check and then calling Wait is the same as calling the method directly: the calling method will not continue until Check is complete. It's possible that the cow-orker doesn't know what he is doing, it's possible that the Check method was used elsewhere and called via a Task without the Wait, but that code caused problems when re-used. We don't know, and will probably never find out. But it's pretty poor code in it's current form!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Thanks. When you say that the cow-orker did not know what he did, you may be right. Next to that code sits a TimeSpan.FromSeconds(SET_XY_MAX_WAIT_TIME_MS) (seconds vs. implied milliseconds ms)... Or he just tries to obfuscate his code so that nobody else can deal with it - job security by obscurity!

        Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

        OriginalGriffO 1 Reply Last reply
        0
        • B Bernhard Hiller

          Thanks. When you say that the cow-orker did not know what he did, you may be right. Next to that code sits a TimeSpan.FromSeconds(SET_XY_MAX_WAIT_TIME_MS) (seconds vs. implied milliseconds ms)... Or he just tries to obfuscate his code so that nobody else can deal with it - job security by obscurity!

          Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          GO to QA - you'll meet a lot of that! :laugh:

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • B Bernhard Hiller

            While I was reading code produced by coworker of mine, I got confused. What does

            public void DoSomething(double someParameter)
            {
            _SomeComponent.Set(someParameter);
            Task.Run(() => Check(someParameter)).Wait();
            }

            actually do, i.e. how does it differ from

            public void DoSomething(double someParameter)
            {
            _SomeComponent.Set(someParameter);
            Check(someParameter);
            }

            The Check function is synchronous. So, the two versions could be equivalent? That Task.Run starts the Check function in a new Task. But because of the Wait, DoSomething won't be left immediately after starting that Task, but only when that Task ran to completion (or failure). But I am too confused by that snippet now to be sure of my assumptions.

            Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

            J Offline
            J Offline
            jsc42
            wrote on last edited by
            #5

            Is it possible that there are other tasks / threads and the author is trying to allow them to have a turn?

            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