├── src
├── TplTipsAndTricks
│ ├── packages.config
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Common
│ │ └── Weather.cs
│ ├── WithTimeout
│ │ ├── TaskEx.cs
│ │ └── Sample.cs
│ ├── ForEachAsync
│ │ ├── Sample.cs
│ │ └── TaskEx.cs
│ ├── ProcessTasksByCompletion
│ │ ├── TaskCompletionSourceEx.cs
│ │ ├── TaskEx.cs
│ │ └── Sample.cs
│ └── TplTipsAndTricks.csproj
└── TplTipsAndTricks.sln
├── LICENSE
├── .gitattributes
└── .gitignore
/src/TplTipsAndTricks/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/TplTipsAndTricks.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.30723.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TplTipsAndTricks", "TplTipsAndTricks\TplTipsAndTricks.csproj", "{53F3A33D-B688-40A7-96BB-B42DBDA618CF}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {53F3A33D-B688-40A7-96BB-B42DBDA618CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {53F3A33D-B688-40A7-96BB-B42DBDA618CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {53F3A33D-B688-40A7-96BB-B42DBDA618CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {53F3A33D-B688-40A7-96BB-B42DBDA618CF}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Sergey Teplyakov
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/src/TplTipsAndTricks/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("TplTipsAndTricks")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("TplTipsAndTricks")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ec62d831-f926-40f5-9d61-9003039163b2")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/src/TplTipsAndTricks/Common/Weather.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 |
4 | namespace TplTipsAndTricks.Common
5 | {
6 | class Weather
7 | {
8 | public Weather(int temperatureCelcius)
9 | {
10 | TemperatureCelcius = temperatureCelcius;
11 | }
12 |
13 | public int TemperatureCelcius { get; private set; }
14 |
15 | public override string ToString()
16 | {
17 | return string.Format("Temp: {0}C", TemperatureCelcius);
18 | }
19 | }
20 |
21 | public class WeatherUnavailableException : Exception
22 | {
23 | public WeatherUnavailableException(string city)
24 | : base(string.Format("Fail to get the weather for '{0}'", city))
25 | {
26 | }
27 | }
28 |
29 | internal static class WeatherService
30 | {
31 | public static Task GetWeatherAsync(string city)
32 | {
33 | return Task.Run(
34 | async () =>
35 | {
36 | await Task.Yield();
37 | Console.WriteLine("Starting getting the weather for '{0}'", city);
38 |
39 | // Faking the temerature by city name length:)
40 |
41 | // Each task should take random amount of time
42 | var interval = 1 + new Random(Guid.NewGuid().GetHashCode()).Next(7);
43 | Console.WriteLine("Sleeping for {0}sec", interval);
44 |
45 | await Task.Delay(TimeSpan.FromSeconds(interval));
46 |
47 | var result = new Weather(city.Length);
48 | Console.WriteLine("Got the weather for '{0}'", city);
49 | return result;
50 | });
51 | }
52 | }
53 |
54 | }
--------------------------------------------------------------------------------
/src/TplTipsAndTricks/WithTimeout/TaskEx.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using TplTipsAndTricks.ProcessTasksByCompletion;
4 |
5 | namespace TplTipsAndTricks.WithTimeout
6 | {
7 | public static class TaskEx
8 | {
9 | public static Task WithTimeout(this Task task, TimeSpan timeout)
10 | {
11 | var tcs = new TaskCompletionSource();
12 |
13 | task.ContinueWith(t =>
14 | {
15 | // This method call will guarantee observe tasks exception!
16 | tcs.FromTask(t);
17 | }, TaskContinuationOptions.ExecuteSynchronously);
18 |
19 | Task.Delay(timeout)
20 | .ContinueWith(t => tcs.TrySetCanceled(),
21 | TaskContinuationOptions.ExecuteSynchronously);
22 |
23 | return tcs.Task;
24 | }
25 |
26 | public static Task WithTimeout(this Task task, TimeSpan timeout)
27 | {
28 | var tcs = new TaskCompletionSource