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


C# CloudBlob.ExistsAsync方法代码示例

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


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

示例1: HandleScheduledService

        private async Task HandleScheduledService(IScheduledService service, CloudBlob blob, CancellationToken token)
        {
            const string lastExecutionMetadata = "LastExecution";
            const string nextExecutionMetadata = "NextExecution";
            while (!token.IsCancellationRequested)
            {
                var nextExecution = DateTime.MinValue;

                // poll: we need to take action if the blob doesn't exist yet, doesn't have a next execution time, or is scheduled to run ASAP
                while (!await blob.ExistsAsync() || String.IsNullOrEmpty(blob.Metadata[nextExecutionMetadata]) || (nextExecution = DateTime.Parse(blob.Metadata[nextExecutionMetadata])) <= DateTime.UtcNow)
                {
                    try
                    {
                        // try to lock the blob
                        using (var @lock = await CloudLock.LockAsync(blob))
                        {
                            if (@lock != null)
                            {
                                // we acquired the lock, which means we're responsible for running the service
                                var lastExecution = String.IsNullOrEmpty(blob.Metadata[lastExecutionMetadata]) ? null : (DateTime?)DateTime.Parse(blob.Metadata[lastExecutionMetadata]);
                                var thisExecution = DateTime.UtcNow;

                                using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token, @lock.CancellationToken))
                                    nextExecution = await service.Run(lastExecution, cts.Token) ?? DateTime.MaxValue;

                                blob.Metadata[lastExecutionMetadata] = thisExecution.ToString(CultureInfo.InvariantCulture);
                                blob.Metadata[nextExecutionMetadata] = nextExecution.ToString(CultureInfo.InvariantCulture);
                                await blob.SetMetadataAsync(@lock.LeaseId);
                            }
                            else
                            {
                                // wait 5 secs to see if it has finished yet
                                await TaskEx.Delay(5000);

                                // since we're going to loop again, we should verify no one wants us to stop looping
                                if (token.IsCancellationRequested)
                                    break;
                            }
                        }

                    }
                    catch (OperationCanceledException)
                    {
                        // if the lock was lost, this will retry everything
                        // if the service was cancelled, this will now bail out entirely
                        break;
                    }
                }

                if (token.IsCancellationRequested)
                    break;

                // either (a) the service didn't need to run, (b) we ran the service, or (c) someone else ran the service
                // now, wait till the next execution time
                var waitTime = nextExecution - DateTime.UtcNow;
                if (waitTime < TimeSpan.Zero)
                    continue;
                if (waitTime > TimeSpan.FromMinutes(5))
                    waitTime = TimeSpan.FromMinutes(5);

                await TaskEx.Delay(waitTime);
            }
        }
开发者ID:bytenik,项目名称:CloudServiceHost,代码行数:63,代码来源:ServiceHost.cs


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