Creating a execution queue by using Task.ContinueWith?

后端 未结 3 762
旧巷少年郎
旧巷少年郎 2021-02-04 16:32

I have several actions that I want to execute in the background, but they have to be executed synchronously one after the other.

I was wondering if it\'s a good idea to

相关标签:
3条回答
  • 2021-02-04 17:10

    I used this snippet and have seem to get it work as designed. The number of instances in my case does not runs in to thousands, but in single digit. Nevertheless, no issues so far.

    I would be interested in the ConcurrentQueue example, if there is any?

    Thanks

    0 讨论(0)
  • 2021-02-04 17:17

    This should work as designed (using the fact that TPL will schedule the continuation immediately if the corresponding task already has completed).

    Personally in this case I would just use a dedicated thread using a concurrent queue (ConcurrentQueue) to draw tasks from - this is more explicit but easier to parse reading the code, especially if you want to find out i.e. how many tasks are currently queued etc.

    0 讨论(0)
  • 2021-02-04 17:37

    There is one flaw with this, which I recently discovered myself because I am also using this method of ensuring tasks execute sequentially.

    In my application I had thousands of instances of these mini-queues and quickly discovered I was having memory issues. Since these queues were often idle I was holding onto the last completed task object for a long time and preventing garbage collection. Since the result object of the last completed task was often over 85,000 bytes it was allocated to Large Object Heap (which does not perform compaction during garbage collection). This resulted in fragmentation of the LOH and the process continuously growing in size.

    As a hack to avoid this, you can schedule a no-op task right after the real one within your lock. For a real solution, I will need to move to a different method of controlling the scheduling.

    0 讨论(0)
提交回复
热议问题