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
On this page of the site you can watch the video online CSharp Async Await Explained: Parallel Processing Tutorial with a duration of hours minute second in good quality, which was uploaded by the user Tech Expert Tutorials 28 January 2024, share the link with friends and acquaintances, this video has already been watched 89 times on youtube and it was liked by 4 viewers. Enjoy your viewing!