Skip to content

Commit

Permalink
Add W5 D4 program
Browse files Browse the repository at this point in the history
  • Loading branch information
cvpfus committed Oct 5, 2023
1 parent 0bbe3ce commit 15a0450
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
9 changes: 9 additions & 0 deletions FormulatrixBootcamp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Task", "W5 D3\Task\Task.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwait", "W5 D3\AsyncAwait\AsyncAwait.csproj", "{CCE2C0C8-CD08-44DA-AD87-32848B8B64A7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W5 D4", "W5 D4", "{5637E846-0989-4572-83A1-6CBC066E2D2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CancellationToken", "W5 D4\CancellationToken\CancellationToken.csproj", "{09D51696-9994-41B5-9959-9DFD1B331B02}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -367,11 +371,16 @@ Global
{CCE2C0C8-CD08-44DA-AD87-32848B8B64A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCE2C0C8-CD08-44DA-AD87-32848B8B64A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CCE2C0C8-CD08-44DA-AD87-32848B8B64A7}.Release|Any CPU.Build.0 = Release|Any CPU
{09D51696-9994-41B5-9959-9DFD1B331B02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09D51696-9994-41B5-9959-9DFD1B331B02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09D51696-9994-41B5-9959-9DFD1B331B02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09D51696-9994-41B5-9959-9DFD1B331B02}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{09D51696-9994-41B5-9959-9DFD1B331B02} = {5637E846-0989-4572-83A1-6CBC066E2D2D}
{CCE2C0C8-CD08-44DA-AD87-32848B8B64A7} = {FC559EAF-E754-4775-A039-64B804F331A8}
{C71461E0-7028-4153-94DC-75B522D07B10} = {FC559EAF-E754-4775-A039-64B804F331A8}
{7A140AEF-0702-44C9-8F9A-972664EB83C9} = {FC559EAF-E754-4775-A039-64B804F331A8}
Expand Down
29 changes: 25 additions & 4 deletions W5 D3/AsyncAwait/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
public class Program
using System.Threading.Tasks;

public class Program
{
static void Main()
{

}
{
Console.WriteLine("Hello");
Task.Run(LoopAsync);
while (true)
{
string? input = Console.ReadLine();
if (input == "x") break;
else Console.WriteLine("Your input: " + input);
}
}

static async Task LoopAsync()
{
await Task.Run(async () =>
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i);
await Task.Delay(1000);
}
});
}
}
10 changes: 10 additions & 0 deletions W5 D4/CancellationToken/CancellationToken.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
29 changes: 29 additions & 0 deletions W5 D4/CancellationToken/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Program
{
static void Main()
{
CancellationTokenSource cts = new();
CancellationToken token = cts.Token;

Task t1 = Task.Run(() => PrintLoop(token));

while (!t1.IsCanceled)
{
if (Console.ReadKey().KeyChar == 'x')
{
cts.Cancel();
break;
}
}
}

static async void PrintLoop(CancellationToken token)
{
int counter = 0;
while (!token.IsCancellationRequested)
{
Console.WriteLine(counter++);
await Task.Delay(1000);
}
}
}

0 comments on commit 15a0450

Please sign in to comment.