1: // Copyright © Microsoft Corporation. All Rights Reserved.
2: // This code released under the terms of the
3: // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
4: // This is sample code only, do not use in production environments
5: //
6: // Part of Visual Studio ALM Rangers - Willy's Cave Dwelling Journals
7:
8: using System;
9: using System.Collections.Generic;
10: using System.Linq;
11: using System.Text;
12: using System.Threading.Tasks;
13: using System.Threading;
14:
15: namespace Microsoft.ALMRangers.Samples.CSharpFeatureTour.Asynchronous
16: {
17: public class AsynchronousTasks
18: {
19: public bool StartWork()
20: {
21: Console.ForegroundColor = ConsoleColor.Red;
22: Console.WriteLine();
23: Console.WriteLine("Starting the weekend chores asynchronously!");
24: Console.WriteLine("------------------------------------------");
25: Console.ForegroundColor = ConsoleColor.Green;
26: return true;
27: }
28:
29: public async Task<bool> DoWork(int milliSeconds)
30: {
31: //Task sleeperTask = Task.Factory.StartNew(()=>Thread.Sleep(milliSeconds));
32: //await sleeperTask;
33:
34: Action<object> action = (object interval) =>
35: {
36: Console.WriteLine("\t\tStart Sleep: {0}", interval);
37: Thread.Sleep((int)interval);
38: Console.WriteLine("\t\tDone Sleeping: {0}", interval);
39: };
40:
41: // Construct an unstarted task
42: Task sleeperTask = new Task(action, milliSeconds);
43: sleeperTask.Start();
44: await sleeperTask;
45:
46: return true;
47: }
48:
49: public async Task<bool> DoLawn()
50: {
51: Console.WriteLine("\tStarting with the Lawn");
52: //var t = this.DoWork(Program.timeLawn);
53: await this.DoWork(Program.timeLawn);
54: Console.WriteLine("\tFinished with the Lawn");
55: return true;
56: }
57:
58: public async Task<bool> DoWeeds()
59: {
60: Console.WriteLine("\tStarting with the Weeds");
61: await this.DoWork(Program.timeWeeds);
62: Console.WriteLine("\tFinished with the Weeds");
63: return true;
64: }
65:
66: public async Task<bool> DoWashing()
67: {
68: Console.WriteLine("\tStarting with the Washing");
69: await this.DoWork(Program.timeWashing);
70: Console.WriteLine("\tFinished with the Washing");
71: return true;
72: }
73:
74: public bool DoAllWork()
75: {
76: // Scenario 1 - Async
77: {
78: Console.WriteLine("\tScenario 1 - Async --------------------");
79: Console.WriteLine();
80:
81: var dateTimeStart = DateTime.Now;
82: Console.WriteLine("\tStarting the chores @ {0}", dateTimeStart.ToString());
83: var t1 = DoLawn();
84: var t2 = DoWeeds();
85: var t3 = DoWashing();
86: Task.WaitAll(t1, t2, t3);
87: var dateTimeStop = DateTime.Now;
88: Console.WriteLine("\tFinished the chores @ {0}", dateTimeStop.ToString());
89:
90: var timeSpan = dateTimeStop - dateTimeStart;
91: Console.WriteLine("\tProcessed for ----> {0}.{1} seconds :)", timeSpan.Seconds, timeSpan.Milliseconds);
92: }
93:
94: // Scenario 2 - Run Async Sync
95: {
96: Console.WriteLine();
97: Console.WriteLine("\tScenario 2 - Run Async Syncc ----------");
98: Console.WriteLine();
99:
100: var dateTimeStart = DateTime.Now;
101: Console.WriteLine("\tStarting the chores @ {0}", dateTimeStart.ToString());
102: DoLawn().Wait();
103: DoWeeds().Wait();
104: DoWashing().Wait();
105: var dateTimeStop = DateTime.Now;
106: Console.WriteLine("\tFinished the chores @ {0}", dateTimeStop.ToString());
107:
108: var timeSpan = dateTimeStop - dateTimeStart;
109: Console.WriteLine("\tProcessed for ----> {0}.{1} seconds :)", timeSpan.Seconds, timeSpan.Milliseconds);
110: }
111:
112:
113: return true;
114: }
115: }
116: }