本文整理汇总了C#中Task.WithCancellation方法的典型用法代码示例。如果您正苦于以下问题:C# Task.WithCancellation方法的具体用法?C# Task.WithCancellation怎么用?C# Task.WithCancellation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Task
的用法示例。
在下文中一共展示了Task.WithCancellation方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadSomeAsync
public async Task<int> ReadSomeAsync(byte[] buffer, int offset, int count, int milliseconds)
{
var task = stream.ReadAsync(buffer, offset, count);
this.task = task;
using (GetTimer(milliseconds))
return await task.WithCancellation(cts.Token).ConfigureAwait(false);
}
示例2: ReadFullyAsync
public async Task ReadFullyAsync(byte[] buffer, int offset, int count, int milliseconds)
{
Func<Task> lambda = async () =>
{
while (count > 0)
{
int delta = await stream.ReadAsync(buffer, offset, count).ConfigureAwait(false);
if (delta == 0) throw new EndOfStreamException();
offset += delta; count -= delta;
}
};
task = lambda();
using (GetTimer(milliseconds))
await task.WithCancellation(cts.Token).ConfigureAwait(false);
}
示例3: FlushAsync
public Task FlushAsync()
{
task = stream.FlushAsync();
return task.WithCancellation(Token);
}
示例4: WriteAsync
public Task WriteAsync(byte[] buffer, int offset, int count)
{
task = stream.WriteAsync(buffer, offset, count);
return task.WithCancellation(Token);
}