CSharp Async Await Explained: Parallel Processing Tutorial

Publicado em: 28 Janeiro 2024
no canal de: Tech Expert Tutorials
89
4

Learn how to use async and await statements in c#:

Unlock the full potential of Asynchronous statements in C# with this tutorial from Tech Expert Tutorials! Learn how to run code in the background and save time. Follow along as we demonstrate how to use asynchronous statements to run multiple functions simultaneously. Don't miss out on this valuable programming technique - subscribe to our channel today!

Related Videos/Playlists:
   • Tutorials and Demos  

Our channel:    / @techexperttutorials  

link to subscribe:    / @techexperttutorials  

Most recent video:    • CSharp Async Await Explained: Parallel Pro...  


Code:
using System;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Net.Http;
using System.Collections.Generic;
using System.Diagnostics;

namespace AsyncAwaitDemo
{
class Program
{
// need to use async Task instead of void to handle async calls
// static void Main(string[], args)
// Task is accessed from here - using System.Threading.Tasks;
static async Task Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
string URL = "https://raw.githubusercontent.com/don...";
// option 1
var tasks = new List Task { RunAsyncTask(), RunAsyncTask("sample.txt"), RunAsyncTask2(URL) };
await Task.WhenAll(tasks);
Console.WriteLine("********************************************************************");
await RunAsyncTask();
string returnText2 = await RunAsyncTask("sample.txt");
Console.WriteLine($"Extracted text from url using version b:\n{returnText2}");
await RunAsyncTask2(URL);
sw.Stop();
Console.WriteLine($"Code ran in {sw.Elapsed.TotalSeconds} seconds");

}
// example without return statement
static async Task RunAsyncTask()
{
Console.WriteLine("Running Task 1 version a ...");
string sampleText = await File.ReadAllTextAsync("sample.txt");
Console.WriteLine($"Extracted Text from local file using version a:\n{sampleText}");
}
// using method overloading - this example returns a string from the method
static async Task string RunAsyncTask(string file_name)
{
Console.WriteLine("Running Task 1 version b ...");
string sampleText = await File.ReadAllTextAsync(file_name);
//Console.WriteLine($"Extracted text from url using version b:\n{sampleText}");
return sampleText;
}
// example using HttpClient from here - using System.Net.Http
static async Task RunAsyncTask2(string URL)
{
Console.WriteLine("Running Task 2 ...");
using(var httpClient = new HttpClient())
{
string result = await httpClient.GetStringAsync(URL);
Console.WriteLine($"Extracted text from url:\n{result}");
}
}
}
}


#async
#await
#tutorial
#CSharp
#demo
#tutorial
#await
#async
#asynchronous
#parallel
#threads
#systemthreads
#tasks
#awaitexplained
#csharpaskasyncawaittutorial


Nesta página do site você pode assistir ao vídeo on-line CSharp Async Await Explained: Parallel Processing Tutorial duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Tech Expert Tutorials 28 Janeiro 2024, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 89 vezes e gostou 4 espectadores. Boa visualização!