C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Watching Asynchronous Operation Execution Status
A tutorial example is provided on how to query the 'Status' property to watch the execution status of an asynchronous operation. The tutorial example shows 5 asynchronous operations running in 5 child threads.
In order to demonstrate the asynchronously of the GetStringAsync() method from the HttpClient class, I wrote another tutorial example program, MultipleAsyncTasks.cs, that starts 5 GetStringAsync() calls and watches their progresses:
// MultipleAsyncTasks.cs // Copyright (c) 2016 HerongYang.com. All Rights Reserved. using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; public class MultipleAsyncTasks { static void Main() { string[] sites = new string[5]; sites[0] = "https://www.herongyang.com/Service/Hello_REST.php"; sites[1] = "http://www.microsoft.com"; sites[2] = "http://www.google.com"; sites[3] = "http://www.faceboot.com"; sites[4] = "http://www.twitter.com"; // Launch 5 asynchronous tasks HttpClient c = new HttpClient(); Task<string>[] tasks = new Task<string>[5]; for (int i=0; i<5; i++) { tasks[i] = c.GetStringAsync(sites[i]); } // Watch their progresses bool wait = true; int w = 0; Console.WriteLine("Time, herongyang.com, microsft.com," +" google.com, facebook, twitter.com"); while (wait && w<=10000) { wait = false; string msg = "" + w; for (int i=0; i<5; i++) { TaskStatus s = tasks[i].Status; wait = wait || (s!=TaskStatus.RanToCompletion); msg = msg+", "+tasks[i].Status; } Console.WriteLine(msg); int t = 100; Thread.Sleep(t); w += t; } } }
Execution output is list below with .NET Framework 4.6.1 SDK:
C:\herong>%NET%\csc "/r:%REF%/System.Net.Http.dll" MultipleAsyncTasks.cs Microsoft (R) Visual C# Compiler version 4.6.1055.0 for C# 5 C:\herong>MultipleAsyncTasks Time, herongyang.com, microsft.com, google.com, facebook, twitter.com 0, WaitingForActivation (WFA), WFA, WFA, WFA, WFA 100, WFA, WFA, WFA, WFA, WFA 200, WFA, WFA, WFA, WFA, WFA 300, WFA, WFA, WFA, WFA, WFA 400, RanToCompletion (RTC), WFA, WFA, WFA, WFA 500, RTC, RTC, WFA, WFA, WFA 600, RTC, RTC, WFA, WFA, WFA 700, RTC, RTC, WFA, WFA, WFA 800, RTC, RTC, WFA, WFA, WFA 900, RTC, RTC, RTC, WFA, WFA 1000, RTC, RTC, RTC, WFA, WFA 1100, RTC, RTC, RTC, WFA, WFA 1200, RTC, RTC, RTC, WFA, WFA 1300, RTC, RTC, RTC, WFA, WFA 1400, RTC, RTC, RTC, WFA, WFA 1500, RTC, RTC, RTC, WFA, WFA 1600, RTC, RTC, RTC, WFA, WFA 1700, RTC, RTC, RTC, WFA, WFA 1800, RTC, RTC, RTC, RTC, WFA 1900, RTC, RTC, RTC, RTC, WFA 2000, RTC, RTC, RTC, RTC, WFA ... 4800, RTC, RTC, RTC, RTC, WFA 4900, RTC, RTC, RTC, RTC, RTC
Excellent! Now we can see asynchronous operation in "slow" motion. Interesting notes about this example program:
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
GetStringAsync() Method in HttpClient Class
GetStringAsync() Method Example Program
►Watching Asynchronous Operation Execution Status
"await" Expression and Child Thread
"await" Expression Thread Example
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation