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


C# TaskCompletionSource.SetFromTask方法代码示例

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


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

示例1: ConvertAsync

        public static Task ConvertAsync(string sourceFileName,
                                        string destinationFileName,
                                        CancellationToken cancellationToken)
        {
            // Wire a graph together.
            var source = File.OpenRead(sourceFileName);
            var decoder = CodecFactory.CreateDecoder(sourceFileName);
            var encoder = CodecFactory.CreateEncoder(destinationFileName);
            var destination = File.Create(destinationFileName);

            const int bufferSize = 16384;

            decoder.ErrorDataReceived += (sender, e) => { Console.WriteLine("{0}: {1}", sourceFileName, e.Data); };
            var decoderTask = decoder.Start(cancellationToken);

            // Note that the process has to be started before you can get the stream,
            // otherwise you get InvalidOperationException containing "StandardIn has not been redirected.".
            var sourceToDecoderTask =
                source.CopyToAsync(decoder.InputStream, bufferSize, cancellationToken)
                      .ContinueWith(t => decoder.InputStream.Close());

            encoder.ErrorDataReceived += (sender, e) => { Console.WriteLine("{0}: {1}", destinationFileName, e.Data); };
            var encoderTask = encoder.Start(cancellationToken);

            var decoderToEncoderTask =
                decoder.OutputStream
                       .CopyToAsync(encoder.InputStream, bufferSize, cancellationToken)
                       .ContinueWith(t => encoder.InputStream.Close());

            var encoderToDestination =
                encoder.OutputStream
                       .CopyToAsync(destination, bufferSize, cancellationToken)
                       .ContinueWith(t => destination.Close());

            // We need to propagate the cancelation/exception; use a TCS.
            var completion = new TaskCompletionSource<bool>();
            Task.WhenAll(sourceToDecoderTask, decoderTask, decoderToEncoderTask, encoderTask, encoderToDestination)
                .ContinueWith(t =>
                    {
                        if (t.IsCanceled || t.IsFaulted)
                        {
                            Console.WriteLine("Deleting '{0}'.", destinationFileName);
                            File.Delete(destinationFileName);
                        }

                        completion.SetFromTask(t);
                    });

            return completion.Task;
        }
开发者ID:rlipscombe,项目名称:nemplode,代码行数:50,代码来源:Transcoder.cs

示例2: _getResponseAsync

 static IEnumerable<Task> _getResponseAsync(TaskCompletionSource<WebResponse> tcs, WebRequest request, TimeSpan timeout)
 {
     using (var cancellation_token = new Concurrency.TimeoutToken(timeout))
     using (var registration_token = cancellation_token.Token.Register(() => { request.Abort(); }))
     {
         using (var task_get_response = request.GetResponseAsync())
         {
             yield return task_get_response;
             tcs.SetFromTask(task_get_response);
             yield break;
         }
     }
 }
开发者ID:AlphaStaxLLC,项目名称:availability-proxy-for-rest-services,代码行数:13,代码来源:Helpers.cs

示例3: EncodeAsync

        public static Task EncodeAsync(string sourceFileName,
                                       string destinationFileName,
                                       CancellationToken cancellationToken)
        {
            // Wire a graph together.
            var source = File.OpenRead(sourceFileName);
            var encoder = CodecFactory.CreateEncoder(destinationFileName);
            var destination = File.Create(destinationFileName);

            const int bufferSize = 16384;

            encoder.ErrorDataReceived += (sender, e) => { Console.WriteLine("{0}: {1}", destinationFileName, e.Data); };
            var encoderTask = encoder.Start(cancellationToken);

            var sourceToEncoderTask =
                source.CopyToAsync(encoder.InputStream, bufferSize, cancellationToken)
                      .ContinueWith(t => encoder.InputStream.Close());

            var encoderToDestination =
                encoder.OutputStream
                       .CopyToAsync(destination, bufferSize, cancellationToken)
                       .ContinueWith(t => destination.Close());

            // We need to propagate the cancelation/exception; use a TCS.
            var completion = new TaskCompletionSource<bool>();
            Task.WhenAll(sourceToEncoderTask, encoderTask, encoderToDestination)
                .ContinueWith(t =>
                {
                    if (t.IsCanceled || t.IsFaulted)
                    {
                        Console.WriteLine("Deleting '{0}'.", destinationFileName);
                        File.Delete(destinationFileName);
                    }

                    completion.SetFromTask(t);
                });

            return completion.Task;
        }
开发者ID:rlipscombe,项目名称:nemplode,代码行数:39,代码来源:Transcoder.cs


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