当前位置: 首页>>代码示例>>C#>>正文


C# TaskCompletionSource.Iterate方法代码示例

本文整理汇总了C#中TaskCompletionSource.Iterate方法的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource.Iterate方法的具体用法?C# TaskCompletionSource.Iterate怎么用?C# TaskCompletionSource.Iterate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TaskCompletionSource的用法示例。


在下文中一共展示了TaskCompletionSource.Iterate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetImplHttp

        private Task GetImplHttp(bool resumeExistingDownload = true)
        {
            var webRequest = (HttpWebRequest) WebRequest.Create(RemoteLocation);
            webRequest.AllowAutoRedirect = true;
            webRequest.Method = WebRequestMethods.Http.Get;

            if (IsCancelled) {
                return CancelledTask();
            }

            return CoTask.Factory.FromAsync<WebResponse>(webRequest.BeginGetResponse, webRequest.EndGetResponse, this).ContinueWithParent(
                    asyncResult => {
                        try {
                            var v = webRequest;

                            if (IsCancelled) {
                                Cancel();
                            }

                            var httpWebResponse = asyncResult.Result as HttpWebResponse;
                            LastStatus = httpWebResponse.StatusCode;

                            if (httpWebResponse.StatusCode == HttpStatusCode.OK) {
                                _lastModified = httpWebResponse.LastModified;
                                _contentLength = httpWebResponse.ContentLength;
                                ActualRemoteLocation = httpWebResponse.ResponseUri;

                                if (IsCancelled) {
                                    Cancel();
                                }

                                GenerateLocalFilename();

                                var filename = httpWebResponse.ContentDispositionFilename();
                                if (!string.IsNullOrEmpty(filename)) {
                                    GenerateLocalFilename(filename);
                                }

                                try {
                                    // we should open the file here, so that it's ready when we start the async read cycle.
                                    if( _filestream != null ) {
                                        throw new Exception("THIS VERY BAD AND UNEXPECTED.");
                                    }
                                    _filestream = File.Open(LocalFullPath, FileMode.Create);

                                    if (IsCancelled) {
                                        Cancel();
                                    }

                                    var tcs = new TaskCompletionSource<HttpWebResponse>(TaskCreationOptions.AttachedToParent);
                                    ((Tasklet) tcs.Task).CancellationToken = Tasklet.CurrentCancellationToken;

                                    tcs.Iterate(AsyncReadImpl(tcs, httpWebResponse));
                                    return;
                                }
                                catch {
                                    // failed to actually create the file, or some other catastrophic failure.

                                    Complete();
                                    return;
                                }
                            }
                            // this is not good.
                            throw new Exception("Status Code other than OK");
                        }
                        catch (WebException e) {
                            try {
                                LastStatus = ((HttpWebResponse) e.Response).StatusCode;
                            }
                            catch (Exception) {
                                // if the fit hits the shan, just call it not found.
                                LastStatus = HttpStatusCode.NotFound;
                            }
                            Complete();
                        }
                        catch (AggregateException ae) {
                            ae = ae.Flatten();
                            var e = ae.InnerExceptions[0] as WebException;

                            if (e != null) {
                                try {
                                    LastStatus = ((HttpWebResponse)e.Response).StatusCode;
                                }
                                catch (Exception) {
                                    // if the fit hits the shan, just call it not found.
                                    LastStatus = HttpStatusCode.NotFound;
                                }
                            }

                            Complete();
                            return;
                        }
                        catch (Exception e) {
                            Console.WriteLine("BAD ERROR: {0}\r\n{1}", e.Message, e.StackTrace);
                            LastStatus = HttpStatusCode.NotFound;
                            Complete();
                            return;
                        }
                        Console.WriteLine("Really? It gets here?");
                        Complete();
//.........这里部分代码省略.........
开发者ID:piscisaureus,项目名称:coapp,代码行数:101,代码来源:RemoteFile.cs

示例2: GetAsync

        public Task GetAsync()
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(RemoteLocation);
            webRequest.AllowAutoRedirect = true;
            webRequest.Method = WebRequestMethods.Http.Get;
            webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            return Task.Factory.FromAsync(webRequest.BeginGetResponse, (Func<IAsyncResult, WebResponse>)webRequest.BetterEndGetResponse, this).ContinueWith(asyncResult => {
                // Logging.Logger.Message("In FromAsync Task::::::{0}", RemoteLocation);
                try {
                    if (_isCanceled) {
                        _failed(RemoteLocation);
                        return;
                    }

                    var httpWebResponse = asyncResult.Result as HttpWebResponse;
                    _lastStatus = httpWebResponse.StatusCode;

                    if (httpWebResponse.StatusCode == HttpStatusCode.OK) {
                        _lastModified = httpWebResponse.LastModified;
                        _contentLength = httpWebResponse.ContentLength;
                        ActualRemoteLocation = httpWebResponse.ResponseUri;

                        if (_isCanceled) {
                            _failed(RemoteLocation);
                            return;
                        }

                        if (string.IsNullOrEmpty(_filename)) {
                            _filename = httpWebResponse.ContentDispositionFilename();

                            if (string.IsNullOrEmpty(_filename)) {
                                _filename = ActualRemoteLocation.LocalPath.Substring(ActualRemoteLocation.LocalPath.LastIndexOf('/') + 1);
                                if (string.IsNullOrEmpty(_filename) || ServerSideExtensions.Contains(Path.GetExtension(_filename))) {
                                    ActualRemoteLocation.GetLeftPart(UriPartial.Path).MakeSafeFileName();
                                }
                            }
                        }

                        try {
                            if (Filename.FileIsLocalAndExists()) {
                                var md5 = string.Empty;
                                try {
                                    if (httpWebResponse.Headers.AllKeys.ContainsIgnoreCase("x-ms-meta-MD5")) {
                                        // it's coming from azure, check the value of the md5 and compare against the file on disk ... better than date/size matching.
                                        md5 = httpWebResponse.Headers["x-ms-meta-MD5"].Trim();
                                    } else if (httpWebResponse.Headers.AllKeys.ContainsIgnoreCase("Content-MD5")) {
                                        md5 = httpWebResponse.Headers["Content-MD5"].Trim();
                                        if (md5.EndsWith("=")) {
                                            md5 = Convert.FromBase64CharArray(md5.ToCharArray(), 0, md5.Length).ToUtf8String();
                                        }
                                    }
                                } catch {
                                    // something gone screwy?
                                }

                                if (!string.IsNullOrEmpty(md5)) {
                                    var localMD5 = string.Empty;
                                    using (var stream = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                                        localMD5 = MD5.Create().ComputeHash(stream).ToHexString();
                                    }

                                    if (string.Equals(md5, localMD5, StringComparison.CurrentCultureIgnoreCase)) {
                                        // it's the same file. We're not doin nothing.
                                        _completed(RemoteLocation);
                                        return;
                                    }

                                    // only do the size/date comparison if the server doesn't provide an MD5
                                } else if (_contentLength > 0 && _lastModified.CompareTo(File.GetCreationTime(Filename)) <= 0 && _contentLength == new FileInfo(Filename).Length) {
                                    // file is identical to the one on disk.
                                    // we're not going to reget it. :p
                                    _completed(RemoteLocation);
                                    return;
                                }
                            }

                            // we should open the file here, so that it's ready when we start the async read cycle.
                            if (_filestream != null) {
                                _failed(RemoteLocation);
                                throw new CoAppException("THIS VERY BAD AND UNEXPECTED. (Failed to close?)");
                            }

                            _filestream = File.Open(Filename, FileMode.Create);

                            if (_isCanceled) {
                                _failed(RemoteLocation);
                                return;
                            }

                            var tcs = new TaskCompletionSource<HttpWebResponse>(TaskCreationOptions.AttachedToParent);
                            tcs.Iterate(AsyncReadImpl(tcs, httpWebResponse));
                            return;
                        } catch {
                            // failed to actually create the file, or some other catastrophic failure.
                            _failed(RemoteLocation);
                            return;
                        }
                    }
                    // this is not good.
//.........这里部分代码省略.........
开发者ID:virmitio,项目名称:coapp,代码行数:101,代码来源:RemoteFile.cs


注:本文中的TaskCompletionSource.Iterate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。