Skip to content

Commit

Permalink
Continuation with child tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
conradakunga committed Sep 24, 2018
1 parent 0fb80c7 commit 0eb942a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Chapter11/09 - Continuation With Child Tasks/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/TaskSample.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
,]
}
15 changes: 15 additions & 0 deletions Chapter11/09 - Continuation With Child Tasks/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/TaskSample.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
41 changes: 41 additions & 0 deletions Chapter11/09 - Continuation With Child Tasks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace TaskSample
{
class Program
{
static void Main(string[] args)
{
var client = new HttpClient();
var bag = new ConcurrentBag<string>();
var task = Task.Factory.StartNew(() =>
{
Task.Factory.StartNew(() =>
{
bag.Add(client.GetStringAsync("https://www.packtpub.com").Result);
}, TaskCreationOptions.AttachedToParent);
Task.Factory.StartNew(() =>
{
bag.Add(client.GetStringAsync("https://www.amazon.com").Result);
}, TaskCreationOptions.AttachedToParent);
Task.Factory.StartNew(() =>
{
bag.Add(client.GetStringAsync("https://www.microhsoft.com").Result);
}, TaskCreationOptions.AttachedToParent);
}
);
var success = task.ContinueWith(prior => Console.WriteLine($"Success! {bag.Count}"),
TaskContinuationOptions.NotOnFaulted);
var error = task.ContinueWith(prior =>
{
var ex = prior.Exception;
Console.WriteLine("Error on fetch!");
}, TaskContinuationOptions.OnlyOnFaulted);
Console.Read();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>

0 comments on commit 0eb942a

Please sign in to comment.