Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit c037b26

Browse files
committed
simplify console writeline lock
1 parent 59a13ea commit c037b26

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public ProxyTestController()
3232
{
3333
if (exception is ProxyHttpException phex)
3434
{
35-
await WriteToConsole(exception.Message + ": " + phex.InnerException?.Message, ConsoleColor.Red);
35+
await WriteToConsole(exception.Message + ": " + phex.InnerException?.Message, true);
3636
}
3737
else
3838
{
39-
await WriteToConsole(exception.Message, ConsoleColor.Red);
39+
await WriteToConsole(exception.Message, true);
4040
}
4141
};
4242
proxyServer.ForwardToUpstreamGateway = true;
@@ -261,41 +261,24 @@ public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs
261261
return Task.FromResult(0);
262262
}
263263

264-
private async Task WriteToConsole(string message)
264+
private async Task WriteToConsole(string message, bool useRedColor = false)
265265
{
266266
await @lock.WaitAsync();
267267

268-
ConsoleColor color;
269-
270-
try
271-
{
272-
color = Console.ForegroundColor;
273-
}
274-
finally
275-
{
276-
@lock.Release();
277-
}
278-
279-
await WriteToConsole(message, color);
280-
}
281-
282-
private async Task WriteToConsole(string message, ConsoleColor color)
283-
{
284-
await @lock.WaitAsync();
285-
286-
ConsoleColor existing = Console.ForegroundColor;
287-
288-
try
268+
if (useRedColor)
289269
{
290-
Console.ForegroundColor = color;
270+
ConsoleColor existing = Console.ForegroundColor;
271+
Console.ForegroundColor = ConsoleColor.Red;
291272
Console.WriteLine(message);
273+
Console.ForegroundColor = existing;
292274
}
293-
finally
275+
else
294276
{
295-
Console.ForegroundColor = existing;
296-
@lock.Release();
277+
Console.WriteLine(message);
297278
}
298279

280+
@lock.Release();
281+
299282
}
300283

301284
///// <summary>

0 commit comments

Comments
 (0)