Exemplo n.º 1
0
        private bool TryGetUrlWithNoTimeValidation(out BaseUrlInfo item, string url, int millisecondsTimeout, CancellationToken cancellationToken)
        {
            this.CheckDisposed();
            item = null;

            if (cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException("The operation was canceled.", cancellationToken);
            }

            var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken,
                                                                                          this._blockinQueueCancellationTokenSource.Token);

            try
            {
                if (millisecondsTimeout != 0)
                {
                    cancellationTokenSource.CancelAfter(millisecondsTimeout);
                    while (true)
                    {
                        if (cancellationTokenSource.IsCancellationRequested)
                        {
                            throw new OperationCanceledException("The operation was canceled.",
                                                                 cancellationTokenSource.Token);
                        }

                        var buffer   = _webClient.DownloadString(url);
                        var urlCount = Extract(buffer).Count();

                        item = new BaseUrlInfo()
                        {
                            Url = url, UrlCount = urlCount
                        };

                        return(true);
                    }
                }
            }
            catch (OperationCanceledException ex)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException("The operation was canceled.", ex);
                }

                return(false);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }

            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                var buffer   = _webClient.DownloadString(url);
                var urlCount = Extract(buffer).Count();

                item = new BaseUrlInfo()
                {
                    Url = url, UrlCount = urlCount
                };

                return(true);
            }
            catch (OperationCanceledException ex)
            {
                throw new OperationCanceledException("The operation was canceled.", ex);
            }
        }
Exemplo n.º 2
0
 public bool TryGetUrl(out BaseUrlInfo item, string url, int millisecondsTimeOut, CancellationToken cancellationToken)
 {
     LinkExtractor.ValidateMillisecondsTimeout(millisecondsTimeOut);
     return(this.TryGetUrlWithNoTimeValidation(out item, url, millisecondsTimeOut, cancellationToken));
 }