asynchrone programmierung beispiele hinzugefügt
parent
c51b7296fa
commit
9d9bacd718
|
@ -0,0 +1,10 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public class Ex3
|
||||||
|
{
|
||||||
|
public static async Task Run()
|
||||||
|
{
|
||||||
|
/* Example using a Task<T> 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<string> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<string> 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<string> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Task<(int index, int waitingTime)>> 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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||||
|
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
|
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
|
||||||
|
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||||
|
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||||
|
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/root/.nuget/packages/</NuGetPackageRoot>
|
||||||
|
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/root/.nuget/packages/</NuGetPackageFolders>
|
||||||
|
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||||
|
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.11.1</NuGetToolVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
|
<SourceRoot Include="/root/.nuget/packages/" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
|
@ -0,0 +1,2 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||||
|
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
|
|
@ -0,0 +1,4 @@
|
||||||
|
// <autogenerated />
|
||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
|
|
@ -0,0 +1,22 @@
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// <auto-generated>
|
||||||
|
// This code was generated by a tool.
|
||||||
|
//
|
||||||
|
// Changes to this file may cause incorrect behavior and will be lost if
|
||||||
|
// the code is regenerated.
|
||||||
|
// </auto-generated>
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
63804ea50179688365807671500045cda9689bbb802dfd4b9f520cad32043daf
|
|
@ -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 =
|
|
@ -0,0 +1,8 @@
|
||||||
|
// <auto-generated/>
|
||||||
|
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;
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
f56eca8a218035aaee0739bd26bba4d23e0f75fd08331f478a25b9179244fa4b
|
|
@ -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
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
735e7f2d774271d1780a96fe641c2f5ec6cf42f9b6bbfa8ed546e26d04a0fe35
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"dgSpecHash": "WpEPBuOGmH8=",
|
||||||
|
"success": true,
|
||||||
|
"projectFilePath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||||
|
"expectedPackageFiles": [],
|
||||||
|
"logs": []
|
||||||
|
}
|
Loading…
Reference in New Issue