Improve the error message when acquiring the interactive semaphore fails

Using the same error message as in the sync version of the `Run` method which is indeed much better than "Could not aquire the interactive semaphore".
This commit is contained in:
Cédric Luthi 2021-10-03 18:47:22 +02:00 committed by Patrik Svensson
parent fa15389158
commit ca2e6ce0ad

View File

@ -15,13 +15,10 @@ namespace Spectre.Console.Internal
public T Run<T>(Func<T> func) public T Run<T>(Func<T> func)
{ {
// Try aquiring the exclusivity semaphore // Try acquiring the exclusivity semaphore
if (!_semaphore.Wait(0)) if (!_semaphore.Wait(0))
{ {
throw new InvalidOperationException( throw CreateExclusivityException();
"Trying to run one or more interactive functions concurrently. " +
"Operations with dynamic displays (e.g. a prompt and a progress display) " +
"cannot be running at the same time.");
} }
try try
@ -36,12 +33,10 @@ namespace Spectre.Console.Internal
public async Task<T> Run<T>(Func<Task<T>> func) public async Task<T> Run<T>(Func<Task<T>> func)
{ {
// Try aquiring the exclusivity semaphore // Try acquiring the exclusivity semaphore
if (!await _semaphore.WaitAsync(0).ConfigureAwait(false)) if (!await _semaphore.WaitAsync(0).ConfigureAwait(false))
{ {
// TODO: Need a better message here throw CreateExclusivityException();
throw new InvalidOperationException(
"Could not aquire the interactive semaphore");
} }
try try
@ -53,5 +48,10 @@ namespace Spectre.Console.Internal
_semaphore.Release(1); _semaphore.Release(1);
} }
} }
private static Exception CreateExclusivityException() => new InvalidOperationException(
"Trying to run one or more interactive functions concurrently. " +
"Operations with dynamic displays (e.g. a prompt and a progress display) " +
"cannot be running at the same time.");
} }
} }