小编xar*_*tal的帖子

取消task.delay无例外或使用异常来控制流量?

我不确定在代码中对事件作出反应的两种可能性.大多数情况下,我担心哪一个需要更少的资源.我有一个观察者注册的方法eventproducer.如果eventproducer返回某个东西,则该方法退出并且该方法的调用者再次启动该方法(您可以将其视为一种长轮询).

eventproducer有时触发大量每秒的事件,有时休息几分钟.

第一种方法是等待500毫秒的延迟,然后检查是否有东西返回或以其他方式(直到超时5分钟)再次延迟500毫秒.

eventProducer.RegisterListener((events)=>{evList.Add(events)});
while(evList.Count=0 && !TimeOut){
    await Task.Delay(500);}
eventProducer.UnRegister(...);
// return evList, method gets recalled immediately
Run Code Online (Sandbox Code Playgroud)

第二种方法是使用a CancellationToken.如果eventproducer产生某些东西,则CancellationTokenSource取消源.在等待的方法中Task.Delay(5min, cancellationToken).

eventProducer.RegisterListener((events)=>{evList.Add(events);
                                          cancelSource.Cancel();}
try
{
     await Task.Delay(5min, cancellationToken)
}
catch(TaskCanceledException){//nothing to do};
eventProducer.UnRegister(...);
// return evList, method gets recalled immediately
Run Code Online (Sandbox Code Playgroud)

第二种方法的优点是,如果生产者生产某种东西,并且我们不必在循环中等待和唤醒,该方法立即返回.

但是,每当制作人产生某种东西时,第二种方法TaskCanceledException就是抛出.我担心这会比清醒更能影响系统负载,等待每500毫秒,尤其是当eventproducer产生大量事件时.

我是否过高估计抛出和捕获异常的成本?有没有办法取消Task.Delay一个CancellationToken但没有投掷TaskCanceledException?IE之类的东西task.setComplete

c# asynchronous exception-handling task-parallel-library async-await

7
推荐指数
1
解决办法
3206
查看次数

CloudBlockBlob:DownloadTextAsync和UploadTextAsync以及元数据/属性

与文档azure docmsdn一样,您必须调用FetchAttributesAsync()以填充CloudBlockBlob的元数据.

我注意到,当我打电话时,元数据也会被填充,DownloadTextAsync()并且在我打电话时写入UploadTextAsync().这是否安全使用,因为它在将来版本的存储sdk中不会改变?我无法找到任何确认该行为的文档.

样品:

        var blob = container.GetBlockBlobReference("testblob");

        blob.Metadata["testdata"] = "set";

        await blob.UploadTextAsync("content");

        blob.Metadata["notset"] = "set";

        blob = container.GetBlockBlobReference("testblob");

        var content = await blob.DownloadTextAsync();
        // here content["testdata"] is set and content["notset"] is null
Run Code Online (Sandbox Code Playgroud)

谢谢你告诉我这件事!

c# azure azure-storage azure-storage-blobs

1
推荐指数
1
解决办法
1621
查看次数