Commit c037b265 authored by justcoding121's avatar justcoding121

simplify console writeline lock

parent 59a13ea6
...@@ -32,11 +32,11 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -32,11 +32,11 @@ namespace Titanium.Web.Proxy.Examples.Basic
{ {
if (exception is ProxyHttpException phex) if (exception is ProxyHttpException phex)
{ {
await WriteToConsole(exception.Message + ": " + phex.InnerException?.Message, ConsoleColor.Red); await WriteToConsole(exception.Message + ": " + phex.InnerException?.Message, true);
} }
else else
{ {
await WriteToConsole(exception.Message, ConsoleColor.Red); await WriteToConsole(exception.Message, true);
} }
}; };
proxyServer.ForwardToUpstreamGateway = true; proxyServer.ForwardToUpstreamGateway = true;
...@@ -261,41 +261,24 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -261,41 +261,24 @@ namespace Titanium.Web.Proxy.Examples.Basic
return Task.FromResult(0); return Task.FromResult(0);
} }
private async Task WriteToConsole(string message) private async Task WriteToConsole(string message, bool useRedColor = false)
{ {
await @lock.WaitAsync(); await @lock.WaitAsync();
ConsoleColor color; if (useRedColor)
try
{
color = Console.ForegroundColor;
}
finally
{
@lock.Release();
}
await WriteToConsole(message, color);
}
private async Task WriteToConsole(string message, ConsoleColor color)
{
await @lock.WaitAsync();
ConsoleColor existing = Console.ForegroundColor;
try
{ {
Console.ForegroundColor = color; ConsoleColor existing = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message); Console.WriteLine(message);
Console.ForegroundColor = existing;
} }
finally else
{ {
Console.ForegroundColor = existing; Console.WriteLine(message);
@lock.Release();
} }
@lock.Release();
} }
///// <summary> ///// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment