PK qL 15-TaskCancellation/PK HDy@λ 15-TaskCancellation/App.config
PK e~Fė
_ 15-TaskCancellation/Program.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
class Program {
static void RunTasks(bool cancel) {
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
var t1 = Task.Run(() => {
Console.WriteLine("Task 1 (in thread {0}): starting wait via Task.Delay().Wait()", Thread.CurrentThread.ManagedThreadId);
Task.Delay(2000, ct).Wait();
Console.WriteLine("Task 1 (in thread {0}): done waiting ...", Thread.CurrentThread.ManagedThreadId);
}, ct);
Thread.Sleep(100);
var t2 = Task.Run(() => {
Console.WriteLine("Task 2 (in thread {0}): starting wait via Thread.Sleep()", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(2000);
Console.WriteLine("Task 2 (in thread {0}): done waiting ...", Thread.CurrentThread.ManagedThreadId);
}, ct);
Thread.Sleep(100);
var t3 = Task.Run(() => {
Console.WriteLine("Task 3 (in thread {0}): starting wait via Task.Delay().ContinueWith()", Thread.CurrentThread.ManagedThreadId);
Task.Delay(3000, ct).ContinueWith(_ => {
Console.WriteLine("Task 3 (in thread {0}): done waiting ...", Thread.CurrentThread.ManagedThreadId);
}, ct);
}, ct);
var t4 = Task.Run(() => {
Console.WriteLine("Task 4 (in thread {0}): starting wait via Task.Delay().ContinueWith(AttachedToParent)", Thread.CurrentThread.ManagedThreadId);
Task.Delay(2000, ct).ContinueWith(_ => {
Console.WriteLine("Task 4 (in thread {0}): done waiting ...", Thread.CurrentThread.ManagedThreadId);
}, ct, TaskContinuationOptions.AttachedToParent, TaskScheduler.Current); // Same as in task 3, as task 4 is not our parent!
}, ct);
if (cancel) {
cts.Cancel();
}
try {
Task.WaitAll(t1, t2, t3, t4);
} catch (Exception ex) {
Console.WriteLine("Main: exception caught from WaitAll:");
Console.WriteLine(ex);
if (ex is AggregateException) {
Console.WriteLine("Main: InnerExceptions.Count: {0}", ((AggregateException) ex).InnerExceptions.Count);
}
}
Console.WriteLine("Main: WaitAll completed ... entering sleep ...");
Thread.Sleep(4000);
Console.WriteLine("Main: done sleeping, exiting ...");
}
static void Main(string[] args) {
RunTasks(false);
Console.WriteLine("--------------");
Console.Write("Press ENTER to repeat test with cancellation ...");
Console.ReadLine();
RunTasks(true);
}
}
PK qL 15-TaskCancellation/Properties/PK HD . 15-TaskCancellation/Properties/AssemblyInfo.csusing System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TaskCancellation")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TaskCancellation")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8d349600-007d-4e1a-9ecc-e6a722b93933")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
PK HDKļ
+ 15-TaskCancellation/TaskCancellation.csproj
Debug
AnyCPU
{12F4EB9E-A44B-4FDE-B293-3830E2709651}
Exe
Properties
TaskCancellation
TaskCancellation
v4.5
512
AnyCPU
true
full
false
bin\Debug\
DEBUG;TRACE
prompt
4
AnyCPU
pdbonly
true
bin\Release\
TRACE
prompt
4
PK HDVs ( 15-TaskCancellation/TaskCancellation.sln
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TaskCancellation", "TaskCancellation.csproj", "{12F4EB9E-A44B-4FDE-B293-3830E2709651}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{12F4EB9E-A44B-4FDE-B293-3830E2709651}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12F4EB9E-A44B-4FDE-B293-3830E2709651}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12F4EB9E-A44B-4FDE-B293-3830E2709651}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12F4EB9E-A44B-4FDE-B293-3830E2709651}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
PK SF8D , 15-TaskCancellation/TaskCancellation.v11.suoࡱ > R o o t E n t r y X%j P r o j I n f o E x V T a s k L i s t U s e r T a s k s $ R W i n d o w M a n a g e r . P i n n e d F r a m e s 6 Q
6 4 5 7 8 9 : ; <