Пример #1
0
        ReseponseFutureProcess(CancellationTokenSource cts, double timeout)
        {
            this._id         = Guid.NewGuid().ToString();
            _tcs             = new TaskCompletionSource <T>();
            _cts             = cts;
            this.OutTimeSpan = timeout;
            var name    = this.Id;
            var pidinfo = ProcessRegistry.Instance.TryAdd(name, this);

            Pid = pidinfo.Item1;


            if (!pidinfo.Item2)
            {
                throw new ProcessNameExistException(name);
            }



            if (cts != null)
            {
                TaskExt.Delay(this.OutTimeSpan, cts.Token)
                .ContinueWith(t =>
                {
                    if (!_tcs.Task.IsCompleted)
                    {
                        _tcs.TrySetException(new TimeoutException("请求超时!"));

                        this.Dispose();
                    }
                });
            }

            Task = _tcs.Task;
        }