本文整理汇总了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;
}
示例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;
}
}
}
示例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;
}