public async Task <string> ReadUserInput(string prompt, int maximumLength, CancellationToken ct) { _services.MainThread().Post(() => { if (!ct.IsCancellationRequested) { _interactiveWindow.Write(prompt); } }); var tcs = new TaskCompletionSource <string>(); Task.Run(() => { using (var reader = _interactiveWindow.ReadStandardInput()) { tcs.TrySetResult(reader?.ReadToEnd() ?? "\n"); } }, ct).DoNotWait(); tcs.RegisterForCancellation(ct).UnregisterOnCompletion(tcs.Task); try { return(await tcs.Task); } catch (OperationCanceledException) { _services.MainThread().Post(() => { if (!ct.IsCancellationRequested) { _interactiveWindow.Operations.TrySubmitStandardInput(); } }); throw; } }
public Task <string> ReadUserInput(string prompt, int maximumLength, CancellationToken ct) { _coreShell.DispatchOnUIThread(() => _interactiveWindow.Write(prompt)); return(Task.Run(() => { using (var reader = _interactiveWindow.ReadStandardInput()) { return reader != null ? Task.FromResult(reader.ReadToEnd()) : Task.FromResult("\n"); } }, ct)); }