Understanding Async / Await
-
I am trying to understand Async, and I have 2 examples, but I don't see the difference: Example 1
static string sampleFile = @"C:\\Projects\\Sandbox\\Async1\\Async1\\somefile.txt"; static void Main(string\[\] args) { Console.WriteLine("Task 1 started"); Task task = new Task(ReadTheFile); task.Start(); task.Wait(); Console.WriteLine("The READ task was started"); Console.ReadLine(); } static void ReadTheFile() { int count = 0; using (StreamReader reader = new StreamReader(sampleFile)) { Console.WriteLine("Reading the file XXXX."); string v = reader.ReadToEnd(); count += v.Length; } Console.WriteLine("Count: " + count); }
Example 2
static void Main(string\[\] args) { Console.WriteLine("Task 1 started"); Task task = new Task(ProcessDataAsync); task.Start(); task.Wait(); Console.WriteLine("The READ task was started"); Console.ReadLine(); } static async void ProcessDataAsync() { Task task = HandleFileAsync(sampleFile); Console.WriteLine("Getting ready to read the file."); int x = await task; Console.WriteLine("Count: " + x); } static async Task HandleFileAsync(string file) { Console.WriteLine("HandleFile enter"); int count = 0; using (StreamReader reader = new StreamReader(file)) { Console.WriteLine("Reading the file."); string v = await reader.ReadToEndAsync(); count += v.Length; } Console.WriteLine("HandleFile exit"); return count; }
They both read in a text file with 1,000,000 lines. The first one actually seems faster. What is the real difference here? Thanks
-
I am trying to understand Async, and I have 2 examples, but I don't see the difference: Example 1
static string sampleFile = @"C:\\Projects\\Sandbox\\Async1\\Async1\\somefile.txt"; static void Main(string\[\] args) { Console.WriteLine("Task 1 started"); Task task = new Task(ReadTheFile); task.Start(); task.Wait(); Console.WriteLine("The READ task was started"); Console.ReadLine(); } static void ReadTheFile() { int count = 0; using (StreamReader reader = new StreamReader(sampleFile)) { Console.WriteLine("Reading the file XXXX."); string v = reader.ReadToEnd(); count += v.Length; } Console.WriteLine("Count: " + count); }
Example 2
static void Main(string\[\] args) { Console.WriteLine("Task 1 started"); Task task = new Task(ProcessDataAsync); task.Start(); task.Wait(); Console.WriteLine("The READ task was started"); Console.ReadLine(); } static async void ProcessDataAsync() { Task task = HandleFileAsync(sampleFile); Console.WriteLine("Getting ready to read the file."); int x = await task; Console.WriteLine("Count: " + x); } static async Task HandleFileAsync(string file) { Console.WriteLine("HandleFile enter"); int count = 0; using (StreamReader reader = new StreamReader(file)) { Console.WriteLine("Reading the file."); string v = await reader.ReadToEndAsync(); count += v.Length; } Console.WriteLine("HandleFile exit"); return count; }
They both read in a text file with 1,000,000 lines. The first one actually seems faster. What is the real difference here? Thanks
Your main problem is in thinking that async is faster. The thing that takes the time is reading the file and both examples do that. The only difference is that in the second example the thread is freed up to other things while it waits for the task to complete. You're only going to see any benefit in doing this if your system is under a lot of load and doing a lot of heavy tasks. Further more, how much benefit you see with asynch processing depends on how many cores etc the machine has. While the asynch tasks may free up your threads, that comes at a cost too so it depends on if the benefit if the asynch code outweights the extra cost. So in your example running a single time you're getting no benefit from the asynch calls, but you're getting the overhead involved in the thread switching about. If you want to time your code to see if there are any benefits to one thing over another then you can use the System.Diagnostics.Stopwatch class.
-
I am trying to understand Async, and I have 2 examples, but I don't see the difference: Example 1
static string sampleFile = @"C:\\Projects\\Sandbox\\Async1\\Async1\\somefile.txt"; static void Main(string\[\] args) { Console.WriteLine("Task 1 started"); Task task = new Task(ReadTheFile); task.Start(); task.Wait(); Console.WriteLine("The READ task was started"); Console.ReadLine(); } static void ReadTheFile() { int count = 0; using (StreamReader reader = new StreamReader(sampleFile)) { Console.WriteLine("Reading the file XXXX."); string v = reader.ReadToEnd(); count += v.Length; } Console.WriteLine("Count: " + count); }
Example 2
static void Main(string\[\] args) { Console.WriteLine("Task 1 started"); Task task = new Task(ProcessDataAsync); task.Start(); task.Wait(); Console.WriteLine("The READ task was started"); Console.ReadLine(); } static async void ProcessDataAsync() { Task task = HandleFileAsync(sampleFile); Console.WriteLine("Getting ready to read the file."); int x = await task; Console.WriteLine("Count: " + x); } static async Task HandleFileAsync(string file) { Console.WriteLine("HandleFile enter"); int count = 0; using (StreamReader reader = new StreamReader(file)) { Console.WriteLine("Reading the file."); string v = await reader.ReadToEndAsync(); count += v.Length; } Console.WriteLine("HandleFile exit"); return count; }
They both read in a text file with 1,000,000 lines. The first one actually seems faster. What is the real difference here? Thanks
Your second example is not the correct way to use
async
. You should avoidasync void
wherever possible: Avoid async void methods[^] You might also get better performance if you use an asynchronousStream
to read the file. Try this:static void Main(string[] args)
{
Console.WriteLine("Task 1 started");Task task = ProcessDataAsync(); task.Wait(); Console.WriteLine("The READ task was started"); Console.ReadLine();
}
static async Task ProcessDataAsync()
{
Task task = HandleFileAsync(sampleFile);Console.WriteLine("Getting ready to read the file."); int x = await task; Console.WriteLine("Count: " + x);
}
static async Task HandleFileAsync(string file)
{
Console.WriteLine("HandleFile enter");
int count = 0;using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous)) using (StreamReader reader = new StreamReader(stream)) { Console.WriteLine("Reading the file."); string v = await reader.ReadToEndAsync(); count += v.Length; } Console.WriteLine("HandleFile exit"); return count;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer