diff --git a/csharp/tutorials/patterns/finished/toll-calculator/Program.cs b/csharp/tutorials/patterns/finished/toll-calculator/Program.cs index d10a3a7e14a..23cb7218143 100644 --- a/csharp/tutorials/patterns/finished/toll-calculator/Program.cs +++ b/csharp/tutorials/patterns/finished/toll-calculator/Program.cs @@ -32,7 +32,7 @@ static void Main(string[] args) Console.WriteLine($"The toll for a three ride share is {tollCalc.CalculateToll(threeRideShare)}"); Console.WriteLine($"The toll for a fullVan is {tollCalc.CalculateToll(fullVan)}"); - Console.WriteLine($"The toll for an mepty taxi is {tollCalc.CalculateToll(emptyTaxi)}"); + Console.WriteLine($"The toll for an empty taxi is {tollCalc.CalculateToll(emptyTaxi)}"); Console.WriteLine($"The toll for a single fare taxi is {tollCalc.CalculateToll(singleFare)}"); Console.WriteLine($"The toll for a double fare taxi is {tollCalc.CalculateToll(doubleFare)}"); Console.WriteLine($"The toll for a full van taxi is {tollCalc.CalculateToll(fullVanPool)}"); @@ -51,7 +51,7 @@ static void Main(string[] args) } catch (ArgumentException e) { - Console.WriteLine("Caught an argument exception when using the wrong type", DayOfWeek.Friday); + Console.WriteLine("Caught an argument exception when using the wrong type"); } try { diff --git a/csharp/tutorials/patterns/start/toll-calculator/Program.cs b/csharp/tutorials/patterns/start/toll-calculator/Program.cs index 232df3b478e..74c7e59405b 100644 --- a/csharp/tutorials/patterns/start/toll-calculator/Program.cs +++ b/csharp/tutorials/patterns/start/toll-calculator/Program.cs @@ -28,7 +28,7 @@ static void Main(string[] args) } catch (ArgumentException e) { - Console.WriteLine("Caught an argument exception when using the wrong type", DayOfWeek.Friday); + Console.WriteLine("Caught an argument exception when using the wrong type"); } try { @@ -64,7 +64,7 @@ static void Main(string[] args) Console.WriteLine($"The toll for a three ride share is {tollCalc.CalculateToll(threeRideShare)}"); Console.WriteLine($"The toll for a fullVan is {tollCalc.CalculateToll(fullVan)}"); - Console.WriteLine($"The toll for an mepty taxi is {tollCalc.CalculateToll(emptyTaxi)}"); + Console.WriteLine($"The toll for an empty taxi is {tollCalc.CalculateToll(emptyTaxi)}"); Console.WriteLine($"The toll for a single fare taxi is {tollCalc.CalculateToll(singleFare)}"); Console.WriteLine($"The toll for a double fare taxi is {tollCalc.CalculateToll(doubleFare)}"); Console.WriteLine($"The toll for a full van taxi is {tollCalc.CalculateToll(fullVanPool)}"); @@ -83,7 +83,7 @@ static void Main(string[] args) } catch (ArgumentException e) { - Console.WriteLine("Caught an argument exception when using the wrong type", DayOfWeek.Friday); + Console.WriteLine("Caught an argument exception when using the wrong type"); } try { diff --git a/snippets/csharp/api/system.diagnostics/process/standarderror/stderror-sync.cs b/snippets/csharp/api/system.diagnostics/process/standarderror/stderror-sync.cs new file mode 100644 index 00000000000..228e64fead6 --- /dev/null +++ b/snippets/csharp/api/system.diagnostics/process/standarderror/stderror-sync.cs @@ -0,0 +1,23 @@ +using System; +using System.Diagnostics; + +public class Example +{ + public static void Main() + { + var p = new Process(); + p.StartInfo.UseShellExecute = false; + p.StartInfo.RedirectStandardError = true; + p.StartInfo.FileName = "Write500Lines.exe"; + p.Start(); + + // To avoid deadlocks, always read the output stream first and then wait. + string output = p.StandardError.ReadToEnd(); + p.WaitForExit(); + + Console.WriteLine($"\nError stream: {output}"); + } +} +// The end of the output produced by the example includes the following: +// Error stream: +// Successfully wrote 500 lines. \ No newline at end of file diff --git a/snippets/csharp/api/system.diagnostics/process/standardoutput/stdoutput-async.cs b/snippets/csharp/api/system.diagnostics/process/standardoutput/stdoutput-async.cs new file mode 100644 index 00000000000..0fc2cfcf1e6 --- /dev/null +++ b/snippets/csharp/api/system.diagnostics/process/standardoutput/stdoutput-async.cs @@ -0,0 +1,34 @@ +using System; +using System.Diagnostics; + +public class Example +{ + public static void Main() + { + var p = new Process(); + p.StartInfo.UseShellExecute = false; + p.StartInfo.RedirectStandardOutput = true; + string eOut = null; + p.StartInfo.RedirectStandardError = true; + p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => + { eOut += e.Data; }); + p.StartInfo.FileName = "Write500Lines.exe"; + p.Start(); + + // To avoid deadlocks, use an asynchronous read operation on at least one of the streams. + p.BeginErrorReadLine(); + string output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + + Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'"); + Console.WriteLine($"\nError stream: {eOut}"); + } +} +// The example displays the following output: +// The last 50 characters in the output stream are: +// ' 49,800.20% +// Line 500 of 500 written: 49,900.20% +// ' +// +// Error stream: Successfully wrote 500 lines. + diff --git a/snippets/csharp/api/system.diagnostics/process/standardoutput/stdoutput-sync.cs b/snippets/csharp/api/system.diagnostics/process/standardoutput/stdoutput-sync.cs new file mode 100644 index 00000000000..570f334e548 --- /dev/null +++ b/snippets/csharp/api/system.diagnostics/process/standardoutput/stdoutput-sync.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics; + +public class Example +{ + public static void Main() + { + var p = new Process(); + p.StartInfo.UseShellExecute = false; + p.StartInfo.RedirectStandardOutput = true; + p.StartInfo.FileName = "Write500Lines.exe"; + p.Start(); + + // To avoid deadlocks, always read the output stream first and then wait. + string output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + + Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'"); + } +} +// The example displays the following output: +// Successfully wrote 500 lines. +// +// The last 50 characters in the output stream are: +// ' 49,800.20% +// Line 500 of 500 written: 49,900.20% +// ' \ No newline at end of file diff --git a/snippets/csharp/api/system.diagnostics/process/standardoutput/write500lines.cs b/snippets/csharp/api/system.diagnostics/process/standardoutput/write500lines.cs new file mode 100644 index 00000000000..c31ffb095fb --- /dev/null +++ b/snippets/csharp/api/system.diagnostics/process/standardoutput/write500lines.cs @@ -0,0 +1,20 @@ +using System; +using System.IO; + +public class Example +{ + public static void Main() + { + for (int ctr = 0; ctr < 500; ctr++) + Console.WriteLine($"Line {ctr + 1} of 500 written: {ctr + 1/500.0:P2}"); + + Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n"); + } +} +// The example displays the following output: +// The last 50 characters in the output stream are: +// ' 49,800.20% +// Line 500 of 500 written: 49,900.20% +//' +// +// Error stream: Successfully wrote 500 lines. \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V2/AsyncBreakfast.csproj b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/AsyncBreakfast.csproj new file mode 100644 index 00000000000..81726ff2f14 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/AsyncBreakfast.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp2.2 + latest + + + diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Bacon.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Bacon.cs new file mode 100644 index 00000000000..28249eaa8d0 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Bacon.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Bacon + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Coffee.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Coffee.cs new file mode 100644 index 00000000000..a3aa2d8f66e --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Coffee.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Coffee + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Egg.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Egg.cs new file mode 100644 index 00000000000..ab41cfdd523 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Egg.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Egg + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Juice.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Juice.cs new file mode 100644 index 00000000000..4e1b7b15fb6 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Juice.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Juice + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Program.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Program.cs new file mode 100644 index 00000000000..6f862ebf522 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Program.cs @@ -0,0 +1,78 @@ +using System; +using System.Threading.Tasks; + +namespace AsyncBreakfast +{ + class Program + { + // + static async Task Main(string[] args) + { + Coffee cup = PourCoffee(); + Console.WriteLine("coffee is ready"); + Egg eggs = await FryEggs(2); + Console.WriteLine("eggs are ready"); + Bacon bacon = await FryBacon(3); + Console.WriteLine("bacon is ready"); + Toast toast = await ToastBread(2); + ApplyButter(toast); + ApplyJam(toast); + Console.WriteLine("toast is ready"); + Juice oj = PourOJ(); + Console.WriteLine("oj is ready"); + + Console.WriteLine("Breakfast is ready!"); + } + // + + private static Juice PourOJ() + { + Console.WriteLine("Pouring Orange Juice"); + return new Juice(); + } + + private static void ApplyJam(Toast toast) => Console.WriteLine("Putting jam on the toast"); + + private static void ApplyButter(Toast toast) => Console.WriteLine("Putting butter on the toast"); + + private static async Task ToastBread(int slices) + { + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("Putting a slice of bread in the toaster"); + Console.WriteLine("Start toasting..."); + await Task.Delay(3000); + Console.WriteLine("Remove toast from toaster"); + return new Toast(); + } + + private static async Task FryBacon(int slices) + { + Console.WriteLine($"putting {slices} of bacon in the pan"); + Console.WriteLine("cooking first side of bacon..."); + await Task.Delay(3000); + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("flipping a slice of bacon"); + Console.WriteLine("cooking the second side of bacon..."); + await Task.Delay(3000); + Console.WriteLine("Put bacon on plate"); + return new Bacon(); + } + + private static async Task FryEggs(int howMany) + { + Console.WriteLine("Warming the egg pan..."); + await Task.Delay(3000); + Console.WriteLine($"cracking {howMany} eggs"); + Console.WriteLine("cooking the eggs ..."); + await Task.Delay(3000); + Console.WriteLine("Put eggs on plate"); + return new Egg(); + } + + private static Coffee PourCoffee() + { + Console.WriteLine("Pouring coffee"); + return new Coffee(); + } + } +} diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Toast.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Toast.cs new file mode 100644 index 00000000000..037857b2187 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V2/Toast.cs @@ -0,0 +1,8 @@ +using System; + +namespace AsyncBreakfast +{ + internal class Toast + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V3/AsyncBreakfast.csproj b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/AsyncBreakfast.csproj new file mode 100644 index 00000000000..81726ff2f14 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/AsyncBreakfast.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp2.2 + latest + + + diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Bacon.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Bacon.cs new file mode 100644 index 00000000000..28249eaa8d0 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Bacon.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Bacon + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Coffee.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Coffee.cs new file mode 100644 index 00000000000..a3aa2d8f66e --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Coffee.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Coffee + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Egg.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Egg.cs new file mode 100644 index 00000000000..ab41cfdd523 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Egg.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Egg + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Juice.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Juice.cs new file mode 100644 index 00000000000..4e1b7b15fb6 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Juice.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Juice + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Program.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Program.cs new file mode 100644 index 00000000000..29295a48706 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Program.cs @@ -0,0 +1,90 @@ +using System; +using System.Threading.Tasks; + +namespace AsyncBreakfast +{ + class Program + { + // + static async Task Main(string[] args) + { + Coffee cup = PourCoffee(); + Console.WriteLine("coffee is ready"); + var eggsTask = FryEggsAsync(2); + var baconTask = FryBaconAsync(3); + var toastTask = makeToastWithButterAndJamAsync(2); + + var eggs = await eggsTask; + Console.WriteLine("eggs are ready"); + var bacon = await baconTask; + Console.WriteLine("bacon is ready"); + var toast = await toastTask; + Console.WriteLine("toast is ready"); + Juice oj = PourOJ(); + Console.WriteLine("oj is ready"); + + Console.WriteLine("Breakfast is ready!"); + + // + async Task makeToastWithButterAndJamAsync(int number) + { + var plainToast = await ToastBreadAsync(number); + ApplyButter(plainToast); + ApplyJam(plainToast); + return plainToast; + } + // + } + // + + private static Juice PourOJ() + { + Console.WriteLine("Pouring Orange Juice"); + return new Juice(); + } + + private static void ApplyJam(Toast toast) => Console.WriteLine("Putting jam on the toast"); + + private static void ApplyButter(Toast toast) => Console.WriteLine("Putting butter on the toast"); + + private static async Task ToastBreadAsync(int slices) + { + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("Putting a slice of bread in the toaster"); + Console.WriteLine("Start toasting..."); + await Task.Delay(3000); + Console.WriteLine("Remove toast from toaster"); + return new Toast(); + } + + private static async Task FryBaconAsync(int slices) + { + Console.WriteLine($"putting {slices} of bacon in the pan"); + Console.WriteLine("cooking first side of bacon..."); + await Task.Delay(3000); + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("flipping a slice of bacon"); + Console.WriteLine("cooking the second side of bacon..."); + await Task.Delay(3000); + Console.WriteLine("Put bacon on plate"); + return new Bacon(); + } + + private static async Task FryEggsAsync(int howMany) + { + Console.WriteLine("Warming the egg pan..."); + await Task.Delay(3000); + Console.WriteLine($"cracking {howMany} eggs"); + Console.WriteLine("cooking the eggs ..."); + await Task.Delay(3000); + Console.WriteLine("Put eggs on plate"); + return new Egg(); + } + + private static Coffee PourCoffee() + { + Console.WriteLine("Pouring coffee"); + return new Coffee(); + } + } +} diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Toast.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Toast.cs new file mode 100644 index 00000000000..037857b2187 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-V3/Toast.cs @@ -0,0 +1,8 @@ +using System; + +namespace AsyncBreakfast +{ + internal class Toast + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-final/AsyncBreakfast.csproj b/snippets/csharp/tour-of-async/AsyncBreakfast-final/AsyncBreakfast.csproj new file mode 100644 index 00000000000..81726ff2f14 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-final/AsyncBreakfast.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp2.2 + latest + + + diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-final/Bacon.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Bacon.cs new file mode 100644 index 00000000000..28249eaa8d0 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Bacon.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Bacon + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-final/Coffee.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Coffee.cs new file mode 100644 index 00000000000..a3aa2d8f66e --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Coffee.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Coffee + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-final/Egg.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Egg.cs new file mode 100644 index 00000000000..ab41cfdd523 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Egg.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Egg + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-final/Juice.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Juice.cs new file mode 100644 index 00000000000..4e1b7b15fb6 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Juice.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Juice + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-final/Program.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Program.cs new file mode 100644 index 00000000000..535a419f157 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Program.cs @@ -0,0 +1,105 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace AsyncBreakfast +{ + class Program + { + // + static async Task Main(string[] args) + { + Coffee cup = PourCoffee(); + Console.WriteLine("coffee is ready"); + var eggsTask = FryEggsAsync(2); + var baconTask = FryBaconAsync(3); + var toastTask = makeToastWithButterAndJamAsync(2); + + // + var allTasks = new List{eggsTask, baconTask, toastTask}; + while (allTasks.Any()) + { + Task finished = await Task.WhenAny(allTasks); + if (finished == eggsTask) + { + Console.WriteLine("eggs are ready"); + allTasks.Remove(eggsTask); + var eggs = await eggsTask; + } else if (finished == baconTask) + { + Console.WriteLine("bacon is ready"); + allTasks.Remove(baconTask); + var bacon = await baconTask; + } else if (finished == toastTask) + { + Console.WriteLine("toast is ready"); + allTasks.Remove(toastTask); + var toast = await toastTask; + } else + allTasks.Remove(finished); + } + Console.WriteLine("Breakfast is ready!"); + // + + async Task makeToastWithButterAndJamAsync(int number) + { + var plainToast = await ToastBreadAsync(number); + ApplyButter(plainToast); + ApplyJam(plainToast); + return plainToast; + } + } + // + + private static Juice PourOJ() + { + Console.WriteLine("Pouring Orange Juice"); + return new Juice(); + } + + private static void ApplyJam(Toast toast) => Console.WriteLine("Putting jam on the toast"); + + private static void ApplyButter(Toast toast) => Console.WriteLine("Putting butter on the toast"); + + private static async Task ToastBreadAsync(int slices) + { + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("Putting a slice of bread in the toaster"); + Console.WriteLine("Start toasting..."); + await Task.Delay(3000); + Console.WriteLine("Remove toast from toaster"); + return new Toast(); + } + + private static async Task FryBaconAsync(int slices) + { + Console.WriteLine($"putting {slices} of bacon in the pan"); + Console.WriteLine("cooking first side of bacon..."); + await Task.Delay(3000); + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("flipping a slice of bacon"); + Console.WriteLine("cooking the second side of bacon..."); + await Task.Delay(3000); + Console.WriteLine("Put bacon on plate"); + return new Bacon(); + } + + private static async Task FryEggsAsync(int howMany) + { + Console.WriteLine("Warming the egg pan..."); + await Task.Delay(3000); + Console.WriteLine($"cracking {howMany} eggs"); + Console.WriteLine("cooking the eggs ..."); + await Task.Delay(3000); + Console.WriteLine("Put eggs on plate"); + return new Egg(); + } + + private static Coffee PourCoffee() + { + Console.WriteLine("Pouring coffee"); + return new Coffee(); + } + } +} diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-final/Toast.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Toast.cs new file mode 100644 index 00000000000..037857b2187 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-final/Toast.cs @@ -0,0 +1,8 @@ +using System; + +namespace AsyncBreakfast +{ + internal class Toast + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-starter/AsyncBreakfast.csproj b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/AsyncBreakfast.csproj new file mode 100644 index 00000000000..81726ff2f14 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/AsyncBreakfast.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp2.2 + latest + + + diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Bacon.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Bacon.cs new file mode 100644 index 00000000000..28249eaa8d0 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Bacon.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Bacon + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Coffee.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Coffee.cs new file mode 100644 index 00000000000..a3aa2d8f66e --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Coffee.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Coffee + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Egg.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Egg.cs new file mode 100644 index 00000000000..ab41cfdd523 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Egg.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Egg + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Juice.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Juice.cs new file mode 100644 index 00000000000..4e1b7b15fb6 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Juice.cs @@ -0,0 +1,6 @@ +namespace AsyncBreakfast +{ + internal class Juice + { + } +} \ No newline at end of file diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Program.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Program.cs new file mode 100644 index 00000000000..c59d8f8035a --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Program.cs @@ -0,0 +1,78 @@ +using System; +using System.Threading.Tasks; + +namespace AsyncBreakfast +{ + class Program + { + // + static void Main(string[] args) + { + Coffee cup = PourCoffee(); + Console.WriteLine("coffee is ready"); + Egg eggs = FryEggs(2); + Console.WriteLine("eggs are ready"); + Bacon bacon = FryBacon(3); + Console.WriteLine("bacon is ready"); + Toast toast = ToastBread(2); + ApplyButter(toast); + ApplyJam(toast); + Console.WriteLine("toast is ready"); + Juice oj = PourOJ(); + Console.WriteLine("oj is ready"); + + Console.WriteLine("Breakfast is ready!"); + } + // + + private static Juice PourOJ() + { + Console.WriteLine("Pouring Orange Juice"); + return new Juice(); + } + + private static void ApplyJam(Toast toast) => Console.WriteLine("Putting jam on the toast"); + + private static void ApplyButter(Toast toast) => Console.WriteLine("Putting butter on the toast"); + + private static Toast ToastBread(int slices) + { + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("Putting a slice of bread in the toaster"); + Console.WriteLine("Start toasting..."); + Task.Delay(3000).Wait(); + Console.WriteLine("Remove toast from toaster"); + return new Toast(); + } + + private static Bacon FryBacon(int slices) + { + Console.WriteLine($"putting {slices} of bacon in the pan"); + Console.WriteLine("cooking first side of bacon..."); + Task.Delay(3000).Wait(); + for (int slice = 0; slice < slices; slice++) + Console.WriteLine("flipping a slice of bacon"); + Console.WriteLine("cooking the second side of bacon..."); + Task.Delay(3000).Wait(); + Console.WriteLine("Put bacon on plate"); + return new Bacon(); + } + + private static Egg FryEggs(int howMany) + { + Console.WriteLine("Warming the egg pan..."); + Task.Delay(3000).Wait(); + Console.WriteLine($"cracking {howMany} eggs"); + Console.WriteLine("cooking the eggs ..."); + Task.Delay(3000).Wait(); + Console.WriteLine("Put eggs on plate"); + return new Egg(); + } + + private static Coffee PourCoffee() + { + Console.WriteLine("Pouring coffee"); + return new Coffee(); + } + } +} diff --git a/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Toast.cs b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Toast.cs new file mode 100644 index 00000000000..037857b2187 --- /dev/null +++ b/snippets/csharp/tour-of-async/AsyncBreakfast-starter/Toast.cs @@ -0,0 +1,8 @@ +using System; + +namespace AsyncBreakfast +{ + internal class Toast + { + } +} \ No newline at end of file diff --git a/snippets/visualbasic/api/system.diagnostics/process/standarderror/stderror-sync.vb b/snippets/visualbasic/api/system.diagnostics/process/standarderror/stderror-sync.vb new file mode 100644 index 00000000000..28f7a1cf292 --- /dev/null +++ b/snippets/visualbasic/api/system.diagnostics/process/standarderror/stderror-sync.vb @@ -0,0 +1,20 @@ +Imports System.Diagnostics' + +Public Module Example + Public Sub Main() + Dim p As New Process() + p.StartInfo.UseShellExecute = False + p.StartInfo.RedirectStandardError = True + p.StartInfo.FileName = "Write500Lines.exe" + p.Start() + + ' To avoid deadlocks, always read the output stream first and then wait. + Dim output As String = p.StandardError.ReadToEnd() + p.WaitForExit() + + Console.WriteLine($"\nError stream: {output}"); + End Sub +End Module +' The end of the output produced by the example includes the following: +' Error stream: +' Successfully wrote 500 lines. \ No newline at end of file diff --git a/snippets/visualbasic/api/system.diagnostics/process/standardoutput/Write500Lines.vb b/snippets/visualbasic/api/system.diagnostics/process/standardoutput/Write500Lines.vb new file mode 100644 index 00000000000..a54b727c121 --- /dev/null +++ b/snippets/visualbasic/api/system.diagnostics/process/standardoutput/Write500Lines.vb @@ -0,0 +1,18 @@ +Imports System.IO + +Public Module Example + Public Sub Main() + For ctr As Integer = 0 To 499 + Console.WriteLine($"Line {ctr + 1} of 500 written: {ctr + 1/500.0:P2}") + Next + + Console.Error.WriteLine($"{vbCrLf}Successfully wrote 500 lines.{vbCrLf}") + End Sub +End Module +' The example displays the following output: +' The last 50 characters in the output stream are: +' ' 49,800.20% +' Line 500 of 500 written: 49,900.20% +' +' +' Error stream: Successfully wrote 500 lines. \ No newline at end of file diff --git a/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-async.vb b/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-async.vb new file mode 100644 index 00000000000..a54515f4a9a --- /dev/null +++ b/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-async.vb @@ -0,0 +1,29 @@ +Imports System.Diagnostics + +Public Module Example + Public Sub Main() + Dim p As New Process() + p.StartInfo.UseShellExecute = False + p.StartInfo.RedirectStandardOutput = True + Dim eOut As String = Nothing + p.StartInfo.RedirectStandardError = True + AddHandler p.ErrorDataReceived, Sub(sender, e) eOut += e.Data + p.StartInfo.FileName = "Write500Lines.exe" + p.Start() + + ' To avoid deadlocks, use an asynchronous read operation on at least one of the streams. + p.BeginErrorReadLine() + Dim output As String = p.StandardOutput.ReadToEnd() + p.WaitForExit() + + Console.WriteLine($"The last 50 characters in the output stream are:{vbCrLf}'{output.Substring(output.Length - 50)}'") + Console.WriteLine($"{vbCrLf}Error stream: {eOut}") + End Sub +End Module +' The example displays the following output: +' The last 50 characters in the output stream are: +' ' 49,800.20% +' Line 500 of 500 written: 49,900.20% +' ' +' +' Error stream: Successfully wrote 500 lines. \ No newline at end of file diff --git a/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-sync.vb b/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-sync.vb new file mode 100644 index 00000000000..432873c665e --- /dev/null +++ b/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-sync.vb @@ -0,0 +1,24 @@ +Imports System.Diagnostics' + +Public Module Example + Public Sub Main() + Dim p As New Process() + p.StartInfo.UseShellExecute = False + p.StartInfo.RedirectStandardOutput = True + p.StartInfo.FileName = "Write500Lines.exe" + p.Start() + + ' To avoid deadlocks, always read the output stream first and then wait. + Dim output As String = p.StandardOutput.ReadToEnd() + p.WaitForExit() + + Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'") + End Sub +End Module +' The example displays the following output: +' Successfully wrote 500 lines. +' +' The last 50 characters in the output stream are: +' ' 49,800.20% +' Line 500 of 500 written: 49,900.20% +' ' \ No newline at end of file