diff --git a/Async_Await/Async_Await.csproj b/Async_Await/Async_Await.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/Async_Await/Async_Await.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/Async_Await/Ex1.cs b/Async_Await/Ex1.cs new file mode 100644 index 0000000..3163499 --- /dev/null +++ b/Async_Await/Ex1.cs @@ -0,0 +1,18 @@ +using System; +using System.Threading.Tasks; + +public class Ex1 +{ + public static async Task Run() + { + /* Example of async and await */ + Console.WriteLine("Start"); + await DoWorkAsync(); + Console.WriteLine("Finished"); + } + + private static async Task DoWorkAsync() + { + await Task.Delay(2000); + } +} \ No newline at end of file diff --git a/Async_Await/Ex2.cs b/Async_Await/Ex2.cs new file mode 100644 index 0000000..5015f33 --- /dev/null +++ b/Async_Await/Ex2.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; + +public class Ex2 +{ + public static async Task Run() + { + /* Example of Tasks, showcasing Ressource Efficency */ + var task1 = DoMoreWorkAsync("One"); + var task2 = DoMoreWorkAsync("Two"); + var task3 = DoMoreWorkAsync("Three"); + + await task1; + await task2; + await task3; + Console.WriteLine("Finished example showcasing resource efficency\n\n"); + } + + private static async Task DoMoreWorkAsync(string name) + { + Console.WriteLine($"Task {name} is being executed by Thread {Thread.CurrentThread.ManagedThreadId}"); + await Task.Delay(2000); + Console.WriteLine($"Task {name} is done"); + + } +} \ No newline at end of file diff --git a/Async_Await/Ex3.cs b/Async_Await/Ex3.cs new file mode 100644 index 0000000..b540922 --- /dev/null +++ b/Async_Await/Ex3.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading.Tasks; + +public class Ex3 +{ + public static async Task Run() + { + /* Example using a Task type */ + var lowerCaseString = "i want to be an upper case string"; + Console.WriteLine("main: " + lowerCaseString); + var upperCaseString = await WorkOnStringAsync(lowerCaseString); + Console.WriteLine("main: " + upperCaseString); + } + + private static async Task WorkOnStringAsync(string s) + { + Console.WriteLine("Task: Async task started"); + Console.WriteLine("Task: Waiting for 2 sec"); + await Task.Delay(2000); + Console.WriteLine("Task: Done waiting, returning the upper case string"); + return s.ToUpper(); + } +} diff --git a/Async_Await/Ex4.cs b/Async_Await/Ex4.cs new file mode 100644 index 0000000..7019055 --- /dev/null +++ b/Async_Await/Ex4.cs @@ -0,0 +1,28 @@ +using System; +using System.Threading.Tasks; + +public class Ex4 +{ + public static async Task Run() + { + /* Example working with Tasks and awaiting them later */ + var lowerCaseString = "i want to be an upper case string"; + Console.WriteLine("main: " + lowerCaseString); + // Start the task but do not await yet + Task upperCaseTask = WorkOnStringAsync(lowerCaseString); + Console.WriteLine("main: WorkOnStringAsync has been called, doing other work..."); + // Now await the result + Console.WriteLine("main: Now waiting for the result"); + var upperCaseString = await upperCaseTask; + Console.WriteLine("main: " + upperCaseString); + } + + private static async Task WorkOnStringAsync(string s) + { + Console.WriteLine("Task: Async task started"); + Console.WriteLine("Task: Waiting for 2 sec"); + await Task.Delay(2000); + Console.WriteLine("Task: Done waiting, returning the upper case string"); + return s.ToUpper(); + } +} \ No newline at end of file diff --git a/Async_Await/Ex5.cs b/Async_Await/Ex5.cs new file mode 100644 index 0000000..ac73a4f --- /dev/null +++ b/Async_Await/Ex5.cs @@ -0,0 +1,27 @@ +using System; +using System.Threading.Tasks; + +public class Ex5 +{ + public static async Task Run() + { + /* Example for Task Coordination using Task.WhenAll() */ + Random random = new(); + async Task<(int index, int waitingTime)> randomDelay(int index) + { + var waitingTime = random.Next(500, 2000); + await Task.Delay(waitingTime); + return (index, waitingTime); + } + + var delayTask1 = randomDelay(1); + var delayTask2 = randomDelay(2); + var delayTask3 = randomDelay(3); + + await Task.WhenAll(delayTask1, delayTask2, delayTask3); + + Console.WriteLine($"Task {delayTask1.Result.index} had to wait for {delayTask1.Result.waitingTime} ms"); + Console.WriteLine($"Task {delayTask2.Result.index} had to wait for {delayTask2.Result.waitingTime} ms"); + Console.WriteLine($"Task {delayTask3.Result.index} had to wait for {delayTask3.Result.waitingTime} ms"); + } +} diff --git a/Async_Await/Ex6.cs b/Async_Await/Ex6.cs new file mode 100644 index 0000000..4836a88 --- /dev/null +++ b/Async_Await/Ex6.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +public class Ex6 +{ + public static async Task Run() + { + /* Example for Task Coordination using Task.WhenAny() */ + Random random = new(); + async Task<(int index, int waitingTime)> randomDelay(int index) + { + var waitingTime = random.Next(500, 2000); + await Task.Delay(waitingTime); + return (index, waitingTime); + } + + List> taskList = new(); + for (int i = 1; i < 10; i++) + { + var delayedTask = randomDelay(i); + taskList.Add(delayedTask); + } + var fastestTask = await Task.WhenAny(taskList); + Console.WriteLine($"The fastest task was Task {fastestTask.Result.index}. It took {fastestTask.Result.waitingTime} ms to complete."); + } +} diff --git a/Async_Await/Program.cs b/Async_Await/Program.cs new file mode 100644 index 0000000..463a035 --- /dev/null +++ b/Async_Await/Program.cs @@ -0,0 +1,40 @@ + +using System; +using System.Threading.Tasks; + +class Program +{ + static async Task Main(string[] args) + { + if (args.Length == 0) + { + Console.WriteLine("Usage: dotnet run -- ex1|ex2|ex3|ex4|ex5|ex6"); + return; + } + + switch (args[0].ToLower()) + { + case "ex1": + await Ex1.Run(); + break; + case "ex2": + await Ex2.Run(); + break; + case "ex3": + await Ex3.Run(); + break; + case "ex4": + await Ex4.Run(); + break; + case "ex5": + await Ex5.Run(); + break; + case "ex6": + await Ex6.Run(); + break; + default: + Console.WriteLine($"Unknown example: {args[0]}"); + break; + } + } +} \ No newline at end of file diff --git a/Async_Await/obj/Async_Await.csproj.nuget.dgspec.json b/Async_Await/obj/Async_Await.csproj.nuget.dgspec.json new file mode 100644 index 0000000..c18fa39 --- /dev/null +++ b/Async_Await/obj/Async_Await.csproj.nuget.dgspec.json @@ -0,0 +1,66 @@ +{ + "format": 1, + "restore": { + "/workspaces/CSharp/Async_Await/Async_Await.csproj": {} + }, + "projects": { + "/workspaces/CSharp/Async_Await/Async_Await.csproj": { + "version": "1.0.0", + "restore": { + "projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj", + "projectName": "Async_Await", + "projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj", + "packagesPath": "/root/.nuget/packages/", + "outputPath": "/workspaces/CSharp/Async_Await/obj/", + "projectStyle": "PackageReference", + "configFilePaths": [ + "/root/.nuget/NuGet/NuGet.Config" + ], + "originalTargetFrameworks": [ + "net8.0" + ], + "sources": { + "https://api.nuget.org/v3/index.json": {} + }, + "frameworks": { + "net8.0": { + "targetAlias": "net8.0", + "projectReferences": {} + } + }, + "warningProperties": { + "warnAsError": [ + "NU1605" + ] + }, + "restoreAuditProperties": { + "enableAudit": "true", + "auditLevel": "low", + "auditMode": "direct" + } + }, + "frameworks": { + "net8.0": { + "targetAlias": "net8.0", + "imports": [ + "net461", + "net462", + "net47", + "net471", + "net472", + "net48", + "net481" + ], + "assetTargetFallback": true, + "warn": true, + "frameworkReferences": { + "Microsoft.NETCore.App": { + "privateAssets": "all" + } + }, + "runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json" + } + } + } + } +} \ No newline at end of file diff --git a/Async_Await/obj/Async_Await.csproj.nuget.g.props b/Async_Await/obj/Async_Await.csproj.nuget.g.props new file mode 100644 index 0000000..112d2bb --- /dev/null +++ b/Async_Await/obj/Async_Await.csproj.nuget.g.props @@ -0,0 +1,15 @@ + + + + True + NuGet + $(MSBuildThisFileDirectory)project.assets.json + /root/.nuget/packages/ + /root/.nuget/packages/ + PackageReference + 6.11.1 + + + + + \ No newline at end of file diff --git a/Async_Await/obj/Async_Await.csproj.nuget.g.targets b/Async_Await/obj/Async_Await.csproj.nuget.g.targets new file mode 100644 index 0000000..3dc06ef --- /dev/null +++ b/Async_Await/obj/Async_Await.csproj.nuget.g.targets @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Async_Await/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs b/Async_Await/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs new file mode 100644 index 0000000..2217181 --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs @@ -0,0 +1,4 @@ +// +using System; +using System.Reflection; +[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")] diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfo.cs b/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfo.cs new file mode 100644 index 0000000..81193bc --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfo.cs @@ -0,0 +1,22 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +using System; +using System.Reflection; + +[assembly: System.Reflection.AssemblyCompanyAttribute("Async_Await")] +[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] +[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] +[assembly: System.Reflection.AssemblyProductAttribute("Async_Await")] +[assembly: System.Reflection.AssemblyTitleAttribute("Async_Await")] +[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] + +// Generated by the MSBuild WriteCodeFragment class. + diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfoInputs.cache b/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfoInputs.cache new file mode 100644 index 0000000..ec2c667 --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfoInputs.cache @@ -0,0 +1 @@ +63804ea50179688365807671500045cda9689bbb802dfd4b9f520cad32043daf diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.GeneratedMSBuildEditorConfig.editorconfig b/Async_Await/obj/Debug/net8.0/Async_Await.GeneratedMSBuildEditorConfig.editorconfig new file mode 100644 index 0000000..ded28be --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/Async_Await.GeneratedMSBuildEditorConfig.editorconfig @@ -0,0 +1,13 @@ +is_global = true +build_property.TargetFramework = net8.0 +build_property.TargetPlatformMinVersion = +build_property.UsingMicrosoftNETSdkWeb = +build_property.ProjectTypeGuids = +build_property.InvariantGlobalization = +build_property.PlatformNeutralAssembly = +build_property.EnforceExtendedAnalyzerRules = +build_property._SupportedPlatformList = Linux,macOS,Windows +build_property.RootNamespace = Async_Await +build_property.ProjectDir = /workspaces/CSharp/Async_Await/ +build_property.EnableComHosting = +build_property.EnableGeneratedComInterfaceComImportInterop = diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.GlobalUsings.g.cs b/Async_Await/obj/Debug/net8.0/Async_Await.GlobalUsings.g.cs new file mode 100644 index 0000000..8578f3d --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/Async_Await.GlobalUsings.g.cs @@ -0,0 +1,8 @@ +// +global using global::System; +global using global::System.Collections.Generic; +global using global::System.IO; +global using global::System.Linq; +global using global::System.Net.Http; +global using global::System.Threading; +global using global::System.Threading.Tasks; diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.assets.cache b/Async_Await/obj/Debug/net8.0/Async_Await.assets.cache new file mode 100644 index 0000000..cf4ca8d Binary files /dev/null and b/Async_Await/obj/Debug/net8.0/Async_Await.assets.cache differ diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.csproj.CoreCompileInputs.cache b/Async_Await/obj/Debug/net8.0/Async_Await.csproj.CoreCompileInputs.cache new file mode 100644 index 0000000..69e2614 --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/Async_Await.csproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +f56eca8a218035aaee0739bd26bba4d23e0f75fd08331f478a25b9179244fa4b diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.csproj.FileListAbsolute.txt b/Async_Await/obj/Debug/net8.0/Async_Await.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..9e03ce1 --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/Async_Await.csproj.FileListAbsolute.txt @@ -0,0 +1,14 @@ +/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await +/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.deps.json +/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.runtimeconfig.json +/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.dll +/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.pdb +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.GeneratedMSBuildEditorConfig.editorconfig +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfoInputs.cache +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfo.cs +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.csproj.CoreCompileInputs.cache +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.dll +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/refint/Async_Await.dll +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.pdb +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.genruntimeconfig.cache +/workspaces/CSharp/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.dll b/Async_Await/obj/Debug/net8.0/Async_Await.dll new file mode 100644 index 0000000..afa1275 Binary files /dev/null and b/Async_Await/obj/Debug/net8.0/Async_Await.dll differ diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.genruntimeconfig.cache b/Async_Await/obj/Debug/net8.0/Async_Await.genruntimeconfig.cache new file mode 100644 index 0000000..c6a1464 --- /dev/null +++ b/Async_Await/obj/Debug/net8.0/Async_Await.genruntimeconfig.cache @@ -0,0 +1 @@ +735e7f2d774271d1780a96fe641c2f5ec6cf42f9b6bbfa8ed546e26d04a0fe35 diff --git a/Async_Await/obj/Debug/net8.0/Async_Await.pdb b/Async_Await/obj/Debug/net8.0/Async_Await.pdb new file mode 100644 index 0000000..230b3d2 Binary files /dev/null and b/Async_Await/obj/Debug/net8.0/Async_Await.pdb differ diff --git a/Async_Await/obj/Debug/net8.0/apphost b/Async_Await/obj/Debug/net8.0/apphost new file mode 100755 index 0000000..7c4e68a Binary files /dev/null and b/Async_Await/obj/Debug/net8.0/apphost differ diff --git a/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll b/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll new file mode 100644 index 0000000..2ef5e72 Binary files /dev/null and b/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll differ diff --git a/Async_Await/obj/Debug/net8.0/refint/Async_Await.dll b/Async_Await/obj/Debug/net8.0/refint/Async_Await.dll new file mode 100644 index 0000000..2ef5e72 Binary files /dev/null and b/Async_Await/obj/Debug/net8.0/refint/Async_Await.dll differ diff --git a/Async_Await/obj/project.assets.json b/Async_Await/obj/project.assets.json new file mode 100644 index 0000000..4513c64 --- /dev/null +++ b/Async_Await/obj/project.assets.json @@ -0,0 +1,71 @@ +{ + "version": 3, + "targets": { + "net8.0": {} + }, + "libraries": {}, + "projectFileDependencyGroups": { + "net8.0": [] + }, + "packageFolders": { + "/root/.nuget/packages/": {} + }, + "project": { + "version": "1.0.0", + "restore": { + "projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj", + "projectName": "Async_Await", + "projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj", + "packagesPath": "/root/.nuget/packages/", + "outputPath": "/workspaces/CSharp/Async_Await/obj/", + "projectStyle": "PackageReference", + "configFilePaths": [ + "/root/.nuget/NuGet/NuGet.Config" + ], + "originalTargetFrameworks": [ + "net8.0" + ], + "sources": { + "https://api.nuget.org/v3/index.json": {} + }, + "frameworks": { + "net8.0": { + "targetAlias": "net8.0", + "projectReferences": {} + } + }, + "warningProperties": { + "warnAsError": [ + "NU1605" + ] + }, + "restoreAuditProperties": { + "enableAudit": "true", + "auditLevel": "low", + "auditMode": "direct" + } + }, + "frameworks": { + "net8.0": { + "targetAlias": "net8.0", + "imports": [ + "net461", + "net462", + "net47", + "net471", + "net472", + "net48", + "net481" + ], + "assetTargetFallback": true, + "warn": true, + "frameworkReferences": { + "Microsoft.NETCore.App": { + "privateAssets": "all" + } + }, + "runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json" + } + } + } +} \ No newline at end of file diff --git a/Async_Await/obj/project.nuget.cache b/Async_Await/obj/project.nuget.cache new file mode 100644 index 0000000..fe2cf43 --- /dev/null +++ b/Async_Await/obj/project.nuget.cache @@ -0,0 +1,8 @@ +{ + "version": 2, + "dgSpecHash": "WpEPBuOGmH8=", + "success": true, + "projectFilePath": "/workspaces/CSharp/Async_Await/Async_Await.csproj", + "expectedPackageFiles": [], + "logs": [] +} \ No newline at end of file