CSharp Async Await Explained: Parallel Processing Tutorial

Pubblicato il: 28 gennaio 2024
sul canale di: 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


In questa pagina del sito puoi guardare il video online CSharp Async Await Explained: Parallel Processing Tutorial della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Tech Expert Tutorials 28 gennaio 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 89 volte e gli è piaciuto 4 spettatori. Buona visione!