/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/asynchronous-programming/snippets/async-scenarios/Program.cs
221 строка
7 KB
Bill Wagner
Xamarin to maui (#40538)
25 апр 2024, 18:27
Не верифицирован
25 апр 2024, 18:27
38ec336
Код
Авторство
О чём код?
using System.Text.RegularExpressions; using System.Windows; using Microsoft.AspNetCore.Mvc; class Button { public Func<object, object, Task>? Clicked { get; internal set; } } class DamageResult { public int Damage { get { return 0; } } } class User { public bool isEnabled { get; set; } public int id { get; set; } } public class Program { private static readonly Button s_downloadButton = new(); private static readonly Button s_calculateButton = new(); private static readonly HttpClient s_httpClient = new(); private static readonly IEnumerable<string> s_urlList = new string[] { "https://learn.microsoft.com", "https://learn.microsoft.com/aspnet/core", "https://learn.microsoft.com/azure", "https://learn.microsoft.com/azure/devops", "https://learn.microsoft.com/dotnet", "https://learn.microsoft.com/dotnet/desktop/wpf/get-started/create-app-visual-studio", "https://learn.microsoft.com/education", "https://learn.microsoft.com/shows/net-core-101/what-is-net", "https://learn.microsoft.com/enterprise-mobility-security", "https://learn.microsoft.com/gaming", "https://learn.microsoft.com/graph", "https://learn.microsoft.com/microsoft-365", "https://learn.microsoft.com/office", "https://learn.microsoft.com/powershell", "https://learn.microsoft.com/sql", "https://learn.microsoft.com/surface", "https://dotnetfoundation.org", "https://learn.microsoft.com/visualstudio", "https://learn.microsoft.com/windows", "https://learn.microsoft.com/maui" }; private static void Calculate() { // <PerformGameCalculation> static DamageResult CalculateDamageDone() { return new DamageResult() { // Code omitted: // // Does an expensive calculation and returns // the result of that calculation. }; } s_calculateButton.Clicked += async (o, e) => { // This line will yield control to the UI while CalculateDamageDone() // performs its work. The UI thread is free to perform other work. var damageResult = await Task.Run(() => CalculateDamageDone()); DisplayDamage(damageResult); }; // </PerformGameCalculation> } private static void DisplayDamage(DamageResult damage) { Console.WriteLine(damage.Damage); } private static void Download(string URL) { // <UnblockingDownload> s_downloadButton.Clicked += async (o, e) => { // This line will yield control to the UI as the request // from the web service is happening. // // The UI thread is now free to perform other work. var stringData = await s_httpClient.GetStringAsync(URL); DoSomethingWithData(stringData); }; // </UnblockingDownload> } private static void DoSomethingWithData(object stringData) { Console.WriteLine("Displaying data: ", stringData); } // <GetUsersForDataset> private static async Task<User> GetUserAsync(int userId) { // Code omitted: // // Given a user Id {userId}, retrieves a User object corresponding // to the entry in the database with {userId} as its Id. return await Task.FromResult(new User() { id = userId }); } private static async Task<IEnumerable<User>> GetUsersAsync(IEnumerable<int> userIds) { var getUserTasks = new List<Task<User>>(); foreach (int userId in userIds) { getUserTasks.Add(GetUserAsync(userId)); } return await Task.WhenAll(getUserTasks); } // </GetUsersForDataset> // <GetUsersForDatasetByLINQ> private static async Task<User[]> GetUsersAsyncByLINQ(IEnumerable<int> userIds) { var getUserTasks = userIds.Select(id => GetUserAsync(id)).ToArray(); return await Task.WhenAll(getUserTasks); } // </GetUsersForDatasetByLINQ> // <ExtractDataFromNetwork> [HttpGet, Route("DotNetCount")] static public async Task<int> GetDotNetCount(string URL) { // Suspends GetDotNetCount() to allow the caller (the web server) // to accept another request, rather than blocking on this one. var html = await s_httpClient.GetStringAsync(URL); return Regex.Matches(html, @"\.NET").Count; } // </ExtractDataFromNetwork> static async Task Main() { Console.WriteLine("Application started."); Console.WriteLine("Counting '.NET' phrase in websites..."); int total = 0; foreach (string url in s_urlList) { var result = await GetDotNetCount(url); Console.WriteLine($"{url}: {result}"); total += result; } Console.WriteLine("Total: " + total); Console.WriteLine("Retrieving User objects with list of IDs..."); IEnumerable<int> ids = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; var users = await GetUsersAsync(ids); foreach (User? user in users) { Console.WriteLine($"{user.id}: isEnabled={user.isEnabled}"); } Console.WriteLine("Application ending."); } } // Example output: // // Application started. // Counting '.NET' phrase in websites... // https://learn.microsoft.com: 0 // https://learn.microsoft.com/aspnet/core: 57 // https://learn.microsoft.com/azure: 1 // https://learn.microsoft.com/azure/devops: 2 // https://learn.microsoft.com/dotnet: 83 // https://learn.microsoft.com/dotnet/desktop/wpf/get-started/create-app-visual-studio: 31 // https://learn.microsoft.com/education: 0 // https://learn.microsoft.com/shows/net-core-101/what-is-net: 42 // https://learn.microsoft.com/enterprise-mobility-security: 0 // https://learn.microsoft.com/gaming: 0 // https://learn.microsoft.com/graph: 0 // https://learn.microsoft.com/microsoft-365: 0 // https://learn.microsoft.com/office: 0 // https://learn.microsoft.com/powershell: 0 // https://learn.microsoft.com/sql: 0 // https://learn.microsoft.com/surface: 0 // https://dotnetfoundation.org: 16 // https://learn.microsoft.com/visualstudio: 0 // https://learn.microsoft.com/windows: 0 // https://learn.microsoft.com/maui: 6 // Total: 238 // Retrieving User objects with list of IDs... // 1: isEnabled= False // 2: isEnabled= False // 3: isEnabled= False // 4: isEnabled= False // 5: isEnabled= False // 6: isEnabled= False // 7: isEnabled= False // 8: isEnabled= False // 9: isEnabled= False // 0: isEnabled= False // Application ending.