-2

I was looking at this MSDN documentation on Task.Run https://msdn.microsoft.com/en-us/library/hh195051(v=vs.110).aspx

One of the complete example look like the following

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      ShowThreadInfo("Application");

      var t = Task.Run(() => ShowThreadInfo("Task") );
      t.Wait();
   }

   static void ShowThreadInfo(String s)
   {
      Console.WriteLine("{0} Thread ID: {1}",
                        s, Thread.CurrentThread.ManagedThreadId);
   }
}

It does not seem to leverage the benefit of asynchronous programming when starting the task and then wait immediately. All MSDN documentation examples on Task.Run, Task.Start, Task.StartNew etc. look like this. Is this just bad documentation or am I missing something?

Yituo
  • 1,458
  • 4
  • 17
  • 26
  • This is more for demonstration purposes. (ie. Showing you that the thread has a different ID) – Blue Aug 02 '18 at 21:47
  • It's just an example of how the threading works with Tasks – Adam H Aug 02 '18 at 21:47
  • If you read the article till the end they explain why they have to use Wait – Steve Aug 02 '18 at 21:49
  • You may want to wait later on and not immediately. The point is that you can do that using the `Wait` method. When to do it depends on the situation. – CodingYoshi Aug 02 '18 at 21:49
  • read this post: https://stackoverflow.com/a/27089652/366064 – Bizhan Aug 02 '18 at 21:56
  • @Steve I did read till the end and understand that Wait is to ensure the task completes. However that was not my question. My question is why the example used to demonstrate async programming actually does not gain anything from running async. From a learning perspective this is a misleading example, and I want clarification if I am misunderstanding. – Yituo Aug 02 '18 at 22:17
  • 1
    Sometimes an example is there to show **what something does** (not 'when is it useful?'). It is a basic example, showing you the basics. The example might not be particularly _useful_, but if they made it more useful it may get more complicated - and thus be teaching you multiple things at once (which will confuse some readers). This is ultimately documentation, **not** a tutorial. Good documentation is hard to write - I think they made the right tradeoff here. If you disagree, click the huge `No` button at the bottom of the help docs. – mjwills Aug 02 '18 at 23:31
  • @Yituo I don't know what to say. MSDN role is to teach the basics when you don't know anything or need a refresh about a class and its sets of methods and, is not, and cannot be a tutorial site. Perhaps you are just past the point where 'basic' documentation is useful for you and probably you need to search something more complex. – Steve Aug 03 '18 at 06:50

1 Answers1

0

Please read the explanatory text around the code like:

The examples show that the asynchronous task executes on a different thread than the main application thread.