I'll try to illustrate my question by the pointless example you see below.
using System;
using System.IO;
namespace PointlessProgram
{
class PointlessClass
{
public static void WriteFooToTextFile ( )
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Hillary\Documents\PointlessFolder\Foo.txt"))
{
file.Write("Foo");
}
}
public static void WriteBarToTextFile ( )
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Hillary\Documents\PointlessFolder\Bar.txt"))
{
file.Write("Bar");
}
}
static void Main()
{
WriteFooToTextFile(); // (A)
WriteBarToTextFile(); // (B)
}
}
}
Here, (B)
does not need to be run after (A)
because it does not depend on any output produced by (A)
, and neighter does (A)
depend on (B)
. Let's suppose my computer has 2 processors and will devote all it's processing power to running this program. Will the compiler figure out that (A)
and (B)
can be run in parallel, or will it run them one after the other unless I explicitely write my code to tell the machine to not wait for (A)
to finish before beginning to execute (B)
? And if so, is async
-await
how I change this execution from
===
A
---
B
===
to
========
A | B
========
???
What I find confusing about async
-await
is that you don't think in terms of partioning your program into independent tasks A
,B
, C
, ...; you instead think in terms of "this task" and "everything else" and all you're doing is saying "everything else can keep running while this task runs".
Please help me understand.