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.Wait() -Why??

Task.Wait() -Why??

Scheduled Pinned Locked Moved C#
csharpvisual-studiowpfcomdesign
11 Posts 4 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.
  • K Kevin Marois

    See [this](https://msdn.microsoft.com/en-us/library/dd235635(v=vs.110).aspx) MSDN example If Task.Wait blocks, why would you ever want to use it?? If I'm in a WPF app and I launch a task from the main thread, it blocks the UI, so why bother using a thread? If I'm going to block the main thread, why not just put the thread's code in the UI to start with? Someone please give me a real example of when I would really want to use Task.Wait. Thanks

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #2

    You are assuming here that the only time that you would want to use a Task is off the UI thread. This isn't the case. As an example, suppose you wrote some code to index files in a particular folder - and you then want to finish the task only once the indexing has completed, you could very well create a top level task and then fire off multiple tasks inside - with a Wait on them completing.

    This space for rent

    K Richard DeemingR 2 Replies Last reply
    0
    • P Pete OHanlon

      You are assuming here that the only time that you would want to use a Task is off the UI thread. This isn't the case. As an example, suppose you wrote some code to index files in a particular folder - and you then want to finish the task only once the indexing has completed, you could very well create a top level task and then fire off multiple tasks inside - with a Wait on them completing.

      This space for rent

      K Offline
      K Offline
      Kevin Marois
      wrote on last edited by
      #3

      So, Task A spawns 3 child tasks, then waits for all 3 to finish. In this case you would want Task A to run async, correct?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      P 1 Reply Last reply
      0
      • K Kevin Marois

        So, Task A spawns 3 child tasks, then waits for all 3 to finish. In this case you would want Task A to run async, correct?

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #4

        Yes.

        This space for rent

        1 Reply Last reply
        0
        • P Pete OHanlon

          You are assuming here that the only time that you would want to use a Task is off the UI thread. This isn't the case. As an example, suppose you wrote some code to index files in a particular folder - and you then want to finish the task only once the indexing has completed, you could very well create a top level task and then fire off multiple tasks inside - with a Wait on them completing.

          This space for rent

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #5

          In that example, it would be better to use Task.WhenAll[^] to wait for the child tasks to complete.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          P 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            In that example, it would be better to use Task.WhenAll[^] to wait for the child tasks to complete.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #6

            That's true. I was trying more to illustrate the point that waiting on a task or a series of tasks doesn't mean that you created it off a UI thread.

            This space for rent

            1 Reply Last reply
            0
            • K Kevin Marois

              See [this](https://msdn.microsoft.com/en-us/library/dd235635(v=vs.110).aspx) MSDN example If Task.Wait blocks, why would you ever want to use it?? If I'm in a WPF app and I launch a task from the main thread, it blocks the UI, so why bother using a thread? If I'm going to block the main thread, why not just put the thread's code in the UI to start with? Someone please give me a real example of when I would really want to use Task.Wait. Thanks

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #7

              There are very few situations where it's necessary to block the current thread until a task has completed. The only obvious one that comes to mind is when you're writing a console application, and you want to use async code. And with .NET 4.5 or higher, it's usually better to call task.GetAwaiter().GetResult() rather than task.Wait(), to avoid having any exceptions wrapped in an AggregateException.

              static class Program
              {
              static void Main()
              {
              MainAsync().GetAwaiter().GetResult();
              }

              static async Task MainAsync()
              {
                  ...
              }
              

              }

              But in most cases, there's usually a better option than blocking the current thread.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              K T 2 Replies Last reply
              0
              • Richard DeemingR Richard Deeming

                There are very few situations where it's necessary to block the current thread until a task has completed. The only obvious one that comes to mind is when you're writing a console application, and you want to use async code. And with .NET 4.5 or higher, it's usually better to call task.GetAwaiter().GetResult() rather than task.Wait(), to avoid having any exceptions wrapped in an AggregateException.

                static class Program
                {
                static void Main()
                {
                MainAsync().GetAwaiter().GetResult();
                }

                static async Task MainAsync()
                {
                    ...
                }
                

                }

                But in most cases, there's usually a better option than blocking the current thread.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #8

                Richard Deeming wrote:

                There are very few situations where it's necessary to block the current thread until a task has completed.

                That's why I asked. I can't see a reason to block, and the example I posted shows the Main method of a console app creating a thread that blocks. It's a typical stupid MSDN example, but I thought I'd ask anyhow. Thanks

                If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                Richard DeemingR 1 Reply Last reply
                0
                • K Kevin Marois

                  Richard Deeming wrote:

                  There are very few situations where it's necessary to block the current thread until a task has completed.

                  That's why I asked. I can't see a reason to block, and the example I posted shows the Main method of a console app creating a thread that blocks. It's a typical stupid MSDN example, but I thought I'd ask anyhow. Thanks

                  If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #9

                  I guess it's difficult to produce a meaningful real-world example that doesn't end up with the concept you're trying to demonstrate buried in unrelated implementation details. :) You just need to imagine that the task is an async method, calling an API that's only available via asynchronous task-returning methods. The Main thread is synchronous, so you're going to have to block at some point to wait for the API calls to complete.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    There are very few situations where it's necessary to block the current thread until a task has completed. The only obvious one that comes to mind is when you're writing a console application, and you want to use async code. And with .NET 4.5 or higher, it's usually better to call task.GetAwaiter().GetResult() rather than task.Wait(), to avoid having any exceptions wrapped in an AggregateException.

                    static class Program
                    {
                    static void Main()
                    {
                    MainAsync().GetAwaiter().GetResult();
                    }

                    static async Task MainAsync()
                    {
                        ...
                    }
                    

                    }

                    But in most cases, there's usually a better option than blocking the current thread.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    T Offline
                    T Offline
                    Tridip Bhattacharjee
                    wrote on last edited by
                    #10

                    this line

                    MainAsync().GetAwaiter().GetResult();

                    block the main thread. thanks

                    tbhattacharjee

                    Richard DeemingR 1 Reply Last reply
                    0
                    • T Tridip Bhattacharjee

                      this line

                      MainAsync().GetAwaiter().GetResult();

                      block the main thread. thanks

                      tbhattacharjee

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #11

                      Yes - which is exactly what I said in my message, if you'd bothered to read it. :doh:


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      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