-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix IOException when Console window unavailable (#2237) #2238
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using System; | ||
| using System.IO; | ||
| using BenchmarkDotNet.Portability; | ||
|
|
||
| namespace BenchmarkDotNet.Running | ||
| { | ||
| /// <summary> | ||
| /// Updates Console.Title, subject to platform capabilities and Console availability. | ||
| /// Restores the original (or fallback) title upon disposal. | ||
| /// </summary> | ||
| internal class ConsoleTitler : IDisposable | ||
| { | ||
| /// <summary> | ||
| /// Whether this instance has any effect. This will be false if the platform doesn't support Console retitling, | ||
| /// or if Console output is redirected. | ||
| /// </summary> | ||
| public bool IsEnabled { get; private set; } | ||
|
|
||
| private string oldConsoleTitle; | ||
|
|
||
| public ConsoleTitler(string initialTitle) | ||
| { | ||
| // Return without enabling if Console output is redirected. | ||
| if (Console.IsOutputRedirected) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| oldConsoleTitle = PlatformSupportsTitleRead() ? Console.Title : ""; | ||
| } | ||
| catch (IOException) | ||
| { | ||
| // We're unable to read Console.Title on a platform that supports it. This can happen when no console | ||
| // window is available due to the application being Windows Forms, WPF, Windows Service or a daemon. | ||
| oldConsoleTitle = ""; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // Enable ConsoleTitler if and only if we can successfully set the Console.Title property. | ||
| Console.Title = initialTitle; | ||
| IsEnabled = true; | ||
| } | ||
| catch (IOException) | ||
| { | ||
| } | ||
| catch (PlatformNotSupportedException) | ||
| { | ||
| // As of .NET 7, platforms other than Windows, Linux and MacOS do not support Console retitling. | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Write the passed in title here. This is where you need the flag to check in
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need a separate try/catch for the write instead of a platform check. We'd want to catch try
{
Console.Title = title;
IsEnabled = true;
}
catch (IOException) { }
catch (PlatformNotSupportedException) { } |
||
| } | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| [System.Runtime.Versioning.SupportedOSPlatformGuard("windows")] | ||
| #endif | ||
| private static bool PlatformSupportsTitleRead() => RuntimeInformation.IsWindows(); | ||
|
|
||
| /// <summary> | ||
| /// Updates Console.Title if enabled. | ||
| /// </summary> | ||
| public void UpdateTitle(string title) | ||
| { | ||
| if (IsEnabled) | ||
| { | ||
| Console.Title = title; | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (IsEnabled) | ||
| { | ||
| Console.Title = oldConsoleTitle; | ||
| IsEnabled = false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure if we want to disable this if output is redirected. I can imagine a scenario where the output is piped to a file, but it would still be useful to see the progress in the title. But not a big deal to me either way.