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


C# IScheduler.Defer方法代码示例

本文整理汇总了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;
        }
开发者ID:oldslowfatstu,项目名称:NLoop,代码行数:50,代码来源:FileInfoExtensions.cs


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