[英]Is Task.Delay(Timeout.Infinite) a non-blocking call and can be used to sleep indefinitely without blocking?
我正在使用Cosmos DB Change Feed庫來處理來自Change Feed的文檔。
var docObserverFactory = DocumentFeedObserverFactory.Create(myCollectionInfo);
await host.RegisterObserverFactoryAsync(docObserverFactory);
Console.WriteLine("Awaiting indefinitely...press Cancel to exit");
// I set the CancellationToken to true on Console.CancelKeyPress Event
await Task.Delay(Timeout.Infinite, cancellationToken);
await host.UnregisterObserversAsync();
在上面的代碼中,在注冊觀察者工廠之后,我需要保持這個線程一段不確定的時間。 因此我使用具有無限超時值的Task.Delay。
問題:
基本上,我想要一個無阻塞的睡眠調用,無限期地等待而不會阻塞。
使用Task.Delay感覺有點奇怪(特別是因為取消,這是預期的,會導致CancellationException
)。 我更喜歡使用SemaphoreSlim
來完成這樣的任務。
private readonly SemaphoreSlim blockUntilFinishedOrCancel = new SemaphoreSlim(0);
//Wait
await this.blockUntilFinishedOrCancel.WaitAsync():
//No More Waiting
this.blockUntilFinishedOrCancel.Release(1);
感謝您的所有答案和評論,通過在互聯網上查看以下有關Change Feed使用的代碼,我意識到這是錯誤的。 特別誤解了“Console.ReadLine”阻塞線程,我需要做同樣的事情。 但是沒有必要阻止此線程取消注冊它。 它可以由任何其他線程完成。
using (DocumentClient destClient = new DocumentClient(destCollInfo.Uri, destCollInfo.MasterKey))
{
DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(destClient, destCollInfo);
ChangeFeedEventHost host = new ChangeFeedEventHost(hostName, documentCollectionLocation, leaseCollectionLocation, feedOptions, feedHostOptions);
await host.RegisterObserverFactoryAsync(docObserverFactory);
// This is where I thought I need to block this thread indeterminately.
Console.WriteLine("Running... Press enter to stop.");
Console.ReadLine();
await host.UnregisterObserversAsync();
}
作為替代方案,我可以將ChangeFeedEventHost作為我的類的一部分,並公開一個方法來取消注冊它。 在CancelKeyPress事件中,我可以調用該unregsiter方法。 這樣我就不需要不確定地睡眠/阻塞線程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.