本文整理匯總了C#中Microsoft.WindowsAzure.StorageClient.CloudBlob.TryAcquireLease方法的典型用法代碼示例。如果您正苦於以下問題:C# CloudBlob.TryAcquireLease方法的具體用法?C# CloudBlob.TryAcquireLease怎麽用?C# CloudBlob.TryAcquireLease使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Microsoft.WindowsAzure.StorageClient.CloudBlob
的用法示例。
在下文中一共展示了CloudBlob.TryAcquireLease方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: AutoRenewLease
public AutoRenewLease(CloudBlob blob)
{
this.blob = blob;
blob.Container.CreateIfNotExist();
try
{
blob.UploadByteArray(new byte[0], new BlobRequestOptions { AccessCondition = AccessCondition.IfNoneMatch("*") });
}
catch (StorageClientException e)
{
if (e.ErrorCode != StorageErrorCode.BlobAlreadyExists
&& e.StatusCode != HttpStatusCode.PreconditionFailed) // 412 from trying to modify a blob that's leased
{
throw;
}
}
leaseId = blob.TryAcquireLease();
if (HasLease)
{
renewalThread = new Thread(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(40));
blob.RenewLease(leaseId);
});
renewalThread.Start();
}
}
示例2: AutoRenewLease
public AutoRenewLease(ILoggerFactory loggerFactory, LoggerLevel logLevel, CloudBlob blob, int renewLeaseSeconds = 40, int leaseLengthSeconds = 90)
{
_logger = loggerFactory.Create(GetType(), logLevel);
var autoRenewLease = this;
_blob = blob;
blob.Container.CreateIfNotExist();
try
{
blob.UploadByteArray(new byte[0], new BlobRequestOptions { AccessCondition = AccessCondition.IfNoneMatch("*")});
}
catch (StorageClientException ex)
{
if (ex.ErrorCode != StorageErrorCode.BlobAlreadyExists)
{
if (ex.StatusCode != HttpStatusCode.PreconditionFailed)
throw;
}
}
LeaseId = blob.TryAcquireLease(leaseLengthSeconds);
if (!HasLease)
return;
_cancellationTokenSource = new CancellationTokenSource();
_resetEvent = new ManualResetEvent(false);
Task.Factory.StartNew(() =>
{
try
{
while (true)
{
_resetEvent.WaitOne(TimeSpan.FromSeconds(renewLeaseSeconds));
if (_cancellationTokenSource.IsCancellationRequested)
break;
blob.RenewLease(autoRenewLease.LeaseId);
}
}
catch (Exception e)
{
LeaseId = null; // Release the lease
_logger.Error("Error renewing blob lease", e);
}
}, _cancellationTokenSource.Token);
}