private static void Pipes_WaitForReadThenTryReadValues() { using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out)) using (AnonymousPipeClientStream clientPipe = new AnonymousPipeClientStream(PipeDirection.In, serverPipe.ClientSafePipeHandle)) { IWritableChannel <int> writer = Channel.WriteToStream <int>(serverPipe); IReadableChannel <int> reader = Channel.ReadFromStream <int>(clientPipe); Task.WaitAll( Task.Run(async() => { for (int i = 0; i < 100; i++) { await writer.WriteAsync(i); } writer.Complete(); Assert.False(writer.TryWrite(100)); }), Task.Run(async() => { int result; int i = 0; while (await reader.WaitToReadAsync()) { if (reader.TryRead(out result)) { Assert.Equal(i++, result); } } Assert.False(reader.TryRead(out result)); })); } }
private static void Pipes_EnumerateValues() { using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out)) using (AnonymousPipeClientStream clientPipe = new AnonymousPipeClientStream(PipeDirection.In, serverPipe.ClientSafePipeHandle)) { IWritableChannel <int> writer = Channel.WriteToStream <int>(serverPipe); IReadableChannel <int> reader = Channel.ReadFromStream <int>(clientPipe); Task.WaitAll( Task.Run(async() => { for (int i = 0; i < 100; i++) { await writer.WriteAsync(i); } writer.Complete(); Assert.False(writer.TryWrite(100)); }), Task.Run(async() => { int i = 0; IAsyncEnumerator <int> e = reader.GetAsyncEnumerator(); while (await e.MoveNextAsync()) { Assert.Equal(i++, e.Current); } })); } }
public static async Task <bool> TryWriteAsync <T>(this IWritableChannel <T> channel, T item, CancellationToken cancellationToken = default) { do { if (channel.TryWrite(item)) { return(true); } } while (await channel.WaitWriteAvailableAsync(cancellationToken).ConfigureAwait(false)); return(false); }
public async Task TryWrite_AfterComplete() { IWritableChannel <int> c = Channel.WriteToStream <int>(new MemoryStream()); Assert.True(c.TryComplete()); Assert.False(c.TryWrite(42)); AssertSynchronousFalse(c.WaitToWriteAsync()); await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.WriteAsync(42)); Assert.False(c.TryComplete()); }
private async Task HandleResponseMessageAsync(TResponse msg) { var header = _protocol.MessageFactory.CreateInvocationMessageReceived(); if (!_sender.TryWrite(header)) { await _sender.WriteOrDisposeAsync(header, CancellationToken).ConfigureAwait(false); } Log.Debug("Sent confirmation about received response of type {0}", msg.GetType().Name); if (!_responses.TryWrite(msg)) { await _responses.WriteAsync(msg, CancellationToken).ConfigureAwait(false); } Log.Debug("Consumed response of type {0}", msg.GetType().Name); }
public override Task <bool> TryInvokeAsync() { if (_channel.TryWrite(_item)) { try { _action(); return(s_trueTask); } catch (Exception exc) { return(Task.FromException <bool>(exc)); } } return(s_falseTask); }
public override Task <bool> TryInvokeAsync() { if (_channel.TryWrite(_item)) { try { return(_action().ContinueWith(t => { t.GetAwaiter().GetResult(); return true; }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); } catch (Exception exc) { return(Task.FromException <bool>(exc)); } } return(s_falseTask); }