本文整理汇总了C#中IScheduler.Defer方法的典型用法代码示例。如果您正苦于以下问题:C# IScheduler.Defer方法的具体用法?C# IScheduler.Defer怎么用?C# IScheduler.Defer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IScheduler
的用法示例。
在下文中一共展示了IScheduler.Defer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadAllBytes
/// <summary>
/// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
/// </summary>
/// <param name="fileInfo">The <see cref="FileInfo"/> from which to read all bytes.</param>
/// <param name="scheduler">The <see cref="IScheduler"/> on which to execute the callbacks.</param>
/// <returns>A <see cref="Promise{TValue}"/> which resolves to a byte array containing the contents of the file.</returns>
/// <exception cref="ArgumentNullException">Thrown if one of the parameters is null.</exception>
public static Promise<byte[]> ReadAllBytes(this FileInfo fileInfo, IScheduler scheduler)
{
// validate arguments
if (fileInfo == null)
throw new ArgumentNullException("fileInfo");
if (scheduler == null)
throw new ArgumentNullException("scheduler");
// create the deferred
var deferred = scheduler.Defer<byte[]>();
// open the file with the async flag
Task.Run(() => {
try
{
byte[] content;
using (var fileStream = fileInfo.OpenRead())
{
var offset = 0;
var length = fileStream.Length;
var count = (int) length;
content = new byte[count];
while (count > 0)
{
var num = fileStream.Read(content, offset, count);
offset += num;
count -= num;
}
}
// resolve the dererred with the read content
deferred.Resolve(content);
}
catch (Exception exception)
{
// reject the deferred with the exception
deferred.Reject(exception);
}
});
// return the promise
return deferred.Promise;
}