示例#1
0
        public T WaitFor <T> (Future <T> future, double?timeout = null)
        {
            if (_IsDisposed)
            {
                throw new ObjectDisposedException("TaskScheduler");
            }

            if (!_JobQueue.CanPumpOnThisThread)
            {
                var evt = future.GetCompletionEvent();
                if (timeout.HasValue)
                {
                    if (evt.Wait((int)(timeout * 1000)))
                    {
                        return(future.Result);
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                else
                {
                    evt.Wait();
                }
            }

            DateTime started = default(DateTime);

            if (timeout.HasValue)
            {
                started = DateTime.UtcNow;
            }

            using (IsActive)
                while (true)
                {
                    if (_JobQueue.WaitForFuture(future))
                    {
                        return(future.Result);
                    }

                    if (timeout.HasValue)
                    {
                        var elapsed = DateTime.UtcNow - started;

                        if (elapsed.TotalSeconds >= timeout)
                        {
                            throw new TimeoutException();
                        }
                    }
                }
        }
示例#2
0
        public T WaitFor <T> (Future <T> future, double?timeout = null)
        {
            if (_IsDisposed)
            {
                throw new ObjectDisposedException("TaskScheduler");
            }

            if (!_JobQueue.CanPumpOnThisThread)
            {
                var evt = future.GetCompletionEvent();
                if (timeout.HasValue)
                {
                    if (evt.Wait((int)(timeout * 1000)))
                    {
                        return(future.Result);
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                else
                {
                    evt.Wait();
                }
            }

            long started = Stopwatch.GetTimestamp();

            using (IsActive)
                while (true)
                {
                    if (_JobQueue.WaitForFuture(future))
                    {
                        return(future.Result);
                    }

                    if (timeout.HasValue)
                    {
                        var elapsed = Stopwatch.GetTimestamp() - started;

                        if ((double)elapsed / Time.SecondInTicks >= timeout)
                        {
                            throw new TimeoutException();
                        }
                    }
                }
        }