BitSharp.Network.RemoteReceiver.WaitForMessage C# (CSharp) Method

WaitForMessage() public method

public WaitForMessage ( bool>.Func predicate, System.TimeSpan timeout ) : Task
predicate bool>.Func
timeout System.TimeSpan
return Task
        public async Task<Message> WaitForMessage(Func<Message, bool> predicate, TimeSpan timeout)
        {
            var messageTcs = new TaskCompletionSource<Message>();
            Action<Peer, Message> handler =
                (_, message) =>
                {
                    if (predicate(message))
                        messageTcs.SetResult(message);
                };

            OnMessage += handler;
            try
            {
                if (await Task.WhenAny(messageTcs.Task, Task.Delay(timeout)) == messageTcs.Task)
                {
                    return await messageTcs.Task;
                }
                else
                {
                    throw new TimeoutException();
                }
            }
            finally
            {
                OnMessage -= handler;
            }
        }

Same methods

RemoteReceiver::WaitForMessage ( bool>.Func predicate, int timeoutMilliseconds ) : Task