-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16f7731
commit ff8ac99
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
Chapter11/05 - Async Downloader With Cancel From Call/.vscode/launch.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
Chapter11/05 - Async Downloader With Cancel From Call/.vscode/tasks.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
49 changes: 49 additions & 0 deletions
49
Chapter11/05 - Async Downloader With Cancel From Call/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace TaskSample | ||
{ | ||
class Program | ||
{ | ||
static async Task Main(string[] args) | ||
{ | ||
try | ||
{ | ||
var source = new CancellationTokenSource(); | ||
source.CancelAfter(TimeSpan.FromSeconds(3)); | ||
var result = await DownloadHomePages(source); | ||
foreach (var item in result) | ||
Console.WriteLine(item); | ||
} | ||
catch (AggregateException ex) | ||
{ | ||
Console.WriteLine(ex.Flatten().InnerException.Message); | ||
} | ||
} | ||
static Task<List<string>> DownloadHomePages(CancellationTokenSource source) | ||
{ | ||
var client = new HttpClient(); | ||
var bag = new List<string>(); | ||
var token = source.Token; | ||
var sites = new string[] {"https://www.packtpub.com","https://www.amazon.com","https://www.google.com", | ||
"https://www.apple.com","https://www.salesforce.com","http://www.microsoft.com","http://www.oracle.com", | ||
"https://www.ibm.com","https://www.redhat.com","https://www.ubuntu.com","https://www.adobe.com", | ||
"https://www.autodesk.com","https://www.embarcadero.com"}; | ||
var task = Task.Factory.StartNew(() => | ||
{ | ||
foreach (var site in sites) | ||
{ | ||
Console.WriteLine($"Processing {site}"); | ||
token.ThrowIfCancellationRequested(); | ||
bag.Add(client.GetStringAsync(site).Result); | ||
} | ||
}, token); | ||
task.Wait(); | ||
return Task.FromResult(bag); | ||
|
||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Chapter11/05 - Async Downloader With Cancel From Call/TaskSample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |