mirror of
https://github.com/PowerShell/PSReadLine
synced 2026-06-08 12:12:13 +00:00
Work around InvalidOperationException from Console API (#3755)
This commit is contained in:
@@ -113,8 +113,40 @@ namespace Microsoft.PowerShell.Internal
|
||||
set { try { Console.OutputEncoding = value; } catch { } }
|
||||
}
|
||||
|
||||
public ConsoleKeyInfo ReadKey() => _readKeyMethod.Value(true);
|
||||
public bool KeyAvailable => Console.KeyAvailable;
|
||||
private static T _TryIgnoreIOE<T>(Func<T> f)
|
||||
{
|
||||
int triesLeft = 10;
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
triesLeft--;
|
||||
return f();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Ignore it. An IOE could be thrown if the "application does not have a
|
||||
// console or when console input has been redirected"... but we don't
|
||||
// expect PSReadLine to be involved in such a situation. So we are
|
||||
// actually probably running into this Issue (wherein another process
|
||||
// attached to the same console terminated at just the right/wrong time):
|
||||
//
|
||||
// https://github.com/dotnet/runtime/issues/88697
|
||||
//
|
||||
// In the event there is some *other* pathological situation
|
||||
// happening, we have limited the number of times we will
|
||||
// swallow/retry this exception/operation.
|
||||
|
||||
if (triesLeft <= 0)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ConsoleKeyInfo ReadKey() => _TryIgnoreIOE(() => _readKeyMethod.Value(true));
|
||||
public bool KeyAvailable => _TryIgnoreIOE(() => Console.KeyAvailable);
|
||||
public void SetWindowPosition(int left, int top) => Console.SetWindowPosition(left, top);
|
||||
public void SetCursorPosition(int left, int top) => Console.SetCursorPosition(left, top);
|
||||
public virtual void Write(string value) => Console.Write(value);
|
||||
|
||||
Reference in New Issue
Block a user