Dot-Net

使用響應式擴展的非同步隊列處理

  • March 12, 2015

有幾篇關於這個的文章,我有這個工作……但我想知道如何一次為我的 Observable 訂閱設置最大數量的任務執行緒。

我有以下內容來並行化日誌條目的非同步保存:

private BlockingCollection<ILogEntry> logEntryQueue;

logEntryQueue = new BlockingCollection<ILogEntry>();
logEntryQueue.GetConsumingEnumerable().ToObservable(Scheduler.TaskPool).Subscribe(SaveLogEntry);

要安排我的保存…但是如何指定調度程序一次使用的最大執行緒數?

這不是 Observable 的功能,而是 Scheduler 的功能。Observable 定義了什麼,調度器定義了where

您需要傳入一個自定義調度程序。一種簡單的方法是繼承 TaskScheduler 並覆蓋“MaximumConcurrencyLevel”屬性。

http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.maximumconcurrencylevel.aspx

我實際上在 MSDN 上找到了一個範例:

http://msdn.microsoft.com/en-us/library/ee789351.aspx

**編輯:**您詢問瞭如何從 TaskScheduler 轉到 IScheduler。另一位開發人員剛剛給了我一點資訊:

var ischedulerForRx = new TaskPoolScheduler
(
   new TaskFactory
   (
       //This is your custom scheduler
       new LimitedConcurrencyLevelTaskScheduler(1)
   )
);

引用自:https://stackoverflow.com/questions/6538321