public Task <Match> WaitForMatch(Regex regex, int timeoutMilliseconds = 0)
        {
            var tcs            = new TaskCompletionSource <Match>();
            var completionLock = new object();

            OnReceivedLineHandler onReceivedLineHandler = null;
            OnStreamClosedHandler onStreamClosedHandler = null;

            onReceivedLineHandler = line =>
            {
                var match = regex.Match(line);
                if (match.Success)
                {
                    lock (completionLock)
                    {
                        if (!tcs.Task.IsCompleted)
                        {
                            OnReceivedLine -= onReceivedLineHandler;
                            OnStreamClosed -= onStreamClosedHandler;
                            tcs.SetResult(match);
                        }
                    }
                }
            };

            onStreamClosedHandler = () =>
            {
                lock (completionLock)
                {
                    if (!tcs.Task.IsCompleted)
                    {
                        OnReceivedLine -= onReceivedLineHandler;
                        OnStreamClosed -= onStreamClosedHandler;
                        tcs.SetException(new EndOfStreamException());
                    }
                }
            };

            OnReceivedLine += onReceivedLineHandler;
            OnStreamClosed += onStreamClosedHandler;

            if (timeoutMilliseconds > 0)
            {
                var timeoutToken = new CancellationTokenSource(timeoutMilliseconds);
                timeoutToken.Token.Register(() =>
                {
                    lock (completionLock)
                    {
                        if (!tcs.Task.IsCompleted)
                        {
                            OnReceivedLine -= onReceivedLineHandler;
                            OnStreamClosed -= onStreamClosedHandler;
                            tcs.SetCanceled();
                        }
                    }
                });
            }

            return(tcs.Task);
        }
        public Task <Match> WaitForMatch(Regex regex, TimeSpan timeout = default)
        {
            var tcs            = new TaskCompletionSource <Match>();
            var completionLock = new object();

            OnReceivedLineHandler onReceivedLineHandler = null;
            OnStreamClosedHandler onStreamClosedHandler = null;

            void ResolveIfStillPending(Action applyResolution)
            {
                lock (completionLock)
                {
                    if (!tcs.Task.IsCompleted)
                    {
                        OnReceivedLine -= onReceivedLineHandler;
                        OnStreamClosed -= onStreamClosedHandler;
                        applyResolution();
                    }
                }
            }

            onReceivedLineHandler = line =>
            {
                var match = regex.Match(line);
                if (match.Success)
                {
                    ResolveIfStillPending(() => tcs.SetResult(match));
                }
            };

            onStreamClosedHandler = () =>
            {
                ResolveIfStillPending(() => tcs.SetException(new EndOfStreamException()));
            };

            OnReceivedLine += onReceivedLineHandler;
            OnStreamClosed += onStreamClosedHandler;

            if (timeout != default)
            {
                var timeoutToken = new CancellationTokenSource(timeout);
                timeoutToken.Token.Register(() =>
                {
                    ResolveIfStillPending(() => tcs.SetCanceled());
                });
            }

            return(tcs.Task);
        }
Exemplo n.º 3
0
        public Task <Match> WaitForMatch(Regex regex)
        {
            var tcs            = new TaskCompletionSource <Match>();
            var completionLock = new object();

            OnReceivedLineHandler onReceivedLineHandler = null;
            OnStreamClosedHandler onStreamClosedHandler = null;

            void ResolveIfStillPending(Action applyResolution)
            {
                lock (completionLock)
                {
                    if (tcs.Task.IsCompleted)
                    {
                        return;
                    }
                    // ReSharper disable AccessToModifiedClosure
                    OnReceivedLine -= onReceivedLineHandler;
                    OnStreamClosed -= onStreamClosedHandler;
                    // ReSharper restore AccessToModifiedClosure
                    applyResolution();
                }
            }

            onReceivedLineHandler = line =>
            {
                Match match = regex.Match(line);
                if (match.Success)
                {
                    ResolveIfStillPending(() => tcs.SetResult(match));
                }
            };

            onStreamClosedHandler = () =>
            {
                ResolveIfStillPending(() => tcs.SetException(new EndOfStreamException()));
            };

            OnReceivedLine += onReceivedLineHandler;
            OnStreamClosed += onStreamClosedHandler;

            return(tcs.Task);
        }
Exemplo n.º 4
0
        public Task<Match> WaitForMatch(Regex regex)
        {
            var tcs = new TaskCompletionSource<Match>();
            var completionLock = new object();

            OnReceivedLineHandler onReceivedLineHandler = null;
            OnStreamClosedHandler onStreamClosedHandler = null;

            void ResolveIfStillPending(Action applyResolution)
            {
                lock (completionLock)
                {
                    if (!tcs.Task.IsCompleted)
                    {
                        OnReceivedLine -= onReceivedLineHandler;
                        OnStreamClosed -= onStreamClosedHandler;
                        applyResolution();
                    }
                }
            }

            onReceivedLineHandler = line =>
            {
                var match = regex.Match(line);
                if (match.Success)
                {
                    ResolveIfStillPending(() => tcs.SetResult(match));
                }
            };

            onStreamClosedHandler = () =>
            {
                ResolveIfStillPending(() => tcs.SetException(new EndOfStreamException()));
            };

            OnReceivedLine += onReceivedLineHandler;
            OnStreamClosed += onStreamClosedHandler;

            return tcs.Task;
        }
Exemplo n.º 5
0
        public Task <string> ReadLine()
        {
            var tcs            = new TaskCompletionSource <string>();
            var completionLock = new object();

            OnReceivedLineHandler onReceivedLineHandler = null;
            OnStreamClosedHandler onStreamClosedHandler = null;

            void ResolveIfStillPending(Action applyResolution)
            {
                lock (completionLock)
                {
                    if (!tcs.Task.IsCompleted)
                    {
                        OnReceivedLine -= onReceivedLineHandler;
                        OnStreamClosed -= onStreamClosedHandler;
                        applyResolution();
                    }
                }
            }

            onReceivedLineHandler = line =>
            {
                ResolveIfStillPending(() => tcs.SetResult(line));
            };

            onStreamClosedHandler = () =>
            {
                ResolveIfStillPending(() => tcs.SetException(new EndOfStreamException()));
            };

            OnReceivedLine += onReceivedLineHandler;
            OnStreamClosed += onStreamClosedHandler;

            return(tcs.Task);
        }
Exemplo n.º 6
0
        private void OnClosed()
        {
            OnStreamClosedHandler dlg = OnStreamClosed;

            dlg?.Invoke();
        }