PK wo░L 14-TaskAsFuturesVS2012/PK \┐qB├y@╬╗ ╗ ! 14-TaskAsFuturesVS2012/App.configя╗┐
PK wo░LЛДt_@ @ ! 14-TaskAsFuturesVS2012/Program.csя╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TasksAsFutures {
class Program {
static Task GetValueAsync(int x) { // since .NET 4 (scheduled into TaskScheduler.Current)
return Task.Factory.StartNew(() => x);
}
static Task GetValueAsync45(int x) { // only in .NET 4.5 (scheduled into TaskScheduler.Default)
return Task.Run(() => x);
}
static Task IncAsync(Task v) {
return Task.Run(() => {
return v.Result + 1;
});
}
static Task Inc2Async(Task v) {
return Task.Run(() => {
v.Wait();
return v.Result + 1;
});
}
static Task IncAlmostOptimalAsync(Task v) {
return v.ContinueWith(_ => v.Result + 1);
}
static Task IncOptimalAsync(Task v) {
return v.ContinueWith(task => task.Result + 1);
}
static Task AddAsync(Task a, Task b) {
return Task.Run(() => {
return a.Result + b.Result;
});
}
static Task Add2Async(Task a, Task b) {
return Task.Run(() => {
Task.WaitAll(a, b);
return a.Result + b.Result;
});
}
static Task AddAlmostOptimalAsync(Task a, Task b) {
return Task.Factory.ContinueWhenAll(new [] { a, b }, _ => {
return a.Result + b.Result;
});
}
static Task AddOptimalAsync(Task a, Task b) {
return Task.Factory.ContinueWhenAll(new [] { a, b }, tasks => {
return tasks[0].Result + tasks[1].Result;
});
}
static void Main(string[] args) {
var a = GetValueAsync(1);
var b = GetValueAsync45(2);
var r1 = AddAsync(
Add2Async(a, GetValueAsync(3)),
IncAsync(Inc2Async(b))
);
var r2 = AddOptimalAsync(
AddAlmostOptimalAsync(a, GetValueAsync(3)),
IncAlmostOptimalAsync(IncOptimalAsync(b))
);
int taskId = Task.WaitAny(r1, r2);
if (taskId == 0) {
PrintAddResult(r1);
PrintOptimalResult(r2);
} else {
PrintOptimalResult(r2);
PrintAddResult(r1);
}
}
static void PrintAddResult(Task t) {
Console.WriteLine("Result: {0}", t.Result);
}
static void PrintOptimalResult(Task t) {
Console.WriteLine("Optimal result: {0}", t.Result);
}
}
}
PK qпL " 14-TaskAsFuturesVS2012/Properties/PK \┐qBI▐┼eк к 1 14-TaskAsFuturesVS2012/Properties/AssemblyInfo.csя╗┐using 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("TaskAsFuturesVS2012")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TaskAsFuturesVS2012")]
[assembly: AssemblyCopyright("Copyright ┬й 2013")]
[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("5e78a3f4-5477-49b1-bf0a-8867f58528aa")]
// 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 `┐qB╧L╗
1 14-TaskAsFuturesVS2012/TaskAsFuturesVS2012.csprojя╗┐
Debug
AnyCPU
{5E614A59-B380-4653-8144-A13070990087}
Exe
Properties
TaskAsFuturesVS2012
TaskAsFuturesVS2012
v4.5
512
AnyCPU
true
full
false
bin\Debug\
DEBUG;TRACE
prompt
4
AnyCPU
pdbonly
true
bin\Release\
TRACE
prompt
4
PK `┐qBp┼dЯ Я . 14-TaskAsFuturesVS2012/TaskAsFuturesVS2012.slnя╗┐
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TaskAsFuturesVS2012", "TaskAsFuturesVS2012.csproj", "{5E614A59-B380-4653-8144-A13070990087}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5E614A59-B380-4653-8144-A13070990087}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E614A59-B380-4653-8144-A13070990087}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E614A59-B380-4653-8144-A13070990087}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E614A59-B380-4653-8144-A13070990087}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
PK рSF, ■V М М 2 14-TaskAsFuturesVS2012/TaskAsFuturesVS2012.v11.suo╨╧рб▒с > ■ ■ R o o t E n t r y ! а└уНk╨ P r o j I n f o E x T a s k L i s t U s e r T a s k s $ { 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 z
¤ ■ ■ 7 1 ■ : 2 3 4 5 6 ■ 8 9 > ; <