Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chapter19 listings #200

Merged
merged 11 commits into from
Dec 20, 2021
Merged
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.RegularExpressions;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_05.Tests
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_03.Tests
{
[TestClass]
public class ProgramTests
Expand All @@ -11,15 +12,19 @@ public void ObservingUnhandledExceptionsWithContinueWith()
string expected = @"Before
Starting...
Continuing A...
Continuing B...
Continuing C...
Continuing {0}...
Continuing {1}...
Finished!";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected,
string actual = IntelliTect.TestTools.Console.ConsoleAssert.Execute(expected,
() =>
{
Program.Main();
});

Assert.IsTrue(
(string.Format(expected, 'B', 'C') == actual.TrimEnd()) ||
(string.Format(expected, 'C', 'B') == actual.TrimEnd())
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_06.Tests
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_04.Tests
{
[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_08.Tests
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_05.Tests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_09.Tests
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_07.Tests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -9,7 +9,15 @@ public class ProgramTests
[TestMethod][Ignore]
public void ValueTaskAsyncReturnTest()
{
string expected = "";
string expected = @"3:0047:Throwing exception.
3:0052:Unhandled exception handler starting.
3:0055:Sleeping for 4000 ms
1:0058:Sleeping for 2000 ms
1:2059:Awake
1:2060:Finally block running.
3:4059:Awake
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown.
";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected,
() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_03
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_01
{
using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_04
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_02
{
using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_05
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_03
{
using System;
using System.Threading.Tasks;
Expand All @@ -17,9 +17,9 @@ public static void Main()
Console.WriteLine("Continuing A..."));
Task taskB = taskA.ContinueWith(antecedent =>
Console.WriteLine("Continuing B..."));
Task taskC = taskB.ContinueWith(antecedent =>
Task taskC = taskA.ContinueWith(antecedent =>
Console.WriteLine("Continuing C..."));
Task.WaitAll(taskC);
Task.WaitAll(taskB, taskC);
Console.WriteLine("Finished!");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_06
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_04
{
using System;
using System.Threading.Tasks;
Expand Down Expand Up @@ -50,9 +50,7 @@ public static void Main()
OnlyOnRanToCompletion);

completedTask.Wait();

}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_07
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_05
{
using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_08
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_06
{
using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_09
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_07
{
using System;
using System.Diagnostics;
using System.Threading;

public class Program
{
public static Stopwatch clock = new Stopwatch();
public static Stopwatch _Clock = new Stopwatch();
public static void Main()
{
ManualResetEventSlim resetEvent = new ManualResetEventSlim();
try
{
clock.Start();

ManualResetEventSlim resetEvent = new ManualResetEventSlim();
_Clock.Start();
// Register a callback to receive notifications
// of any unhandled exception
#if NETCOREAPP1_1
Expand All @@ -38,13 +37,12 @@ void Work()
GC.WaitForPendingFinalizers();
GC.Collect();


#else
Action<object, EventArgs> unhandledExcpetionHandler = (s, e) =>
{
Message("Event handler starting");
Delay(4000);
};
#else
AppDomain.CurrentDomain.UnhandledException +=
(s, e) =>
{
Expand All @@ -57,18 +55,16 @@ void Work()
throw new Exception();
});
thread.Start();

#endif
#if DEBUG1
resetEvent.Wait();
{
#else
if (!resetEvent.Wait(5000))
{
throw new Exception("Timed out waiting for unhandled exception.");
#endif // DEBUG
}
//Delay(2000)
#endif // DEBUG
Delay(2000);
}
finally
{
Expand All @@ -85,8 +81,7 @@ static void Delay(int i)

static void Message(string text)
{
Console.WriteLine(
$"{Thread.CurrentThread.ManagedThreadId}:{text}");
Console.WriteLine("{0}:{1:0000}:{2}",Thread.CurrentThread.ManagedThreadId,_Clock.ElapsedMilliseconds, text);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_10
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_08
{
using System;
using System.Threading;
Expand All @@ -15,7 +15,6 @@ public static void Main()

CancellationTokenSource cancellationTokenSource =
new CancellationTokenSource();

// Use Task.Factory.StartNew<string>() for
// TPL prior to .NET 4.5
Task task = Task.Run(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_11
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_09
{
using System.Threading.Tasks;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_12
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter19.Listing19_10
{
using System;
using System.Threading;
Expand Down