Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,42 @@ public async static Task AsynchronousExceptionHandlingAsync()
Console.WriteLine(ex.Message);
}
}

public async static Task NonBlockingWaitExamplesAsync()
{
Console.WriteLine("** Non-Blocking Wait Examples **");

Task<string> singleTask = Task<string>.Run(() =>
{
Thread.Sleep(10);

return "Hello, from a single awaited task";
});

string singleResult = await singleTask;
Console.WriteLine(singleResult);

Task<string> firstTask = Task<string>.Run(() =>
{
Thread.Sleep(10);

return "Hello, from the first task";
});

Task<string> secondTask = Task<string>.Run(() =>
{
Thread.Sleep(20);

return "Hello, from the second task";
});

string[] allResults = await Task.WhenAll(firstTask, secondTask);
Console.WriteLine($"Both tasks finished: {string.Join(", ", allResults)}");

Task<string> firstToFinish = await Task.WhenAny(firstTask, secondTask);
Console.WriteLine($"First task to finish: {firstToFinish.Result}");

await Task.Delay(10);
Console.WriteLine("Waited 10ms without blocking the thread");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
CodeExamples.BlockingCodeExample();
await CodeExamples.AsynchronousCodeExampleAsync();

await CodeExamples.NonBlockingWaitExamplesAsync();

await CodeExamples.AsynchronousExceptionHandlingAsync();
CodeExamples.BlockingExceptionHandling();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ namespace Tests;
public class UnitTests
{
[TestMethod]
[ExpectedException(typeof(AggregateException))]
public void GivenAFailingTask_WhenTaskWaitIsCalled_ThenAggregateExceptionIsRaised()
{
CodeExamples.BlockingExceptionHandling();
Assert.ThrowsExactly<AggregateException>(() => CodeExamples.BlockingExceptionHandling());
}
}
Loading