简体   繁体   English

TaskScheduler仍需要处理的任务数

[英]TaskScheduler Number of tasks that still need to be processed

I have a pool of servers that is used to load balance some work. 我有一个服务器池,用于负载平衡某些工作。 This work is partitioned in discrete work items, however it's impossible to determine how much time a given work item will take. 这项工作被划分为离散的工作项,但是无法确定给定工作项将花费多少时间。

Each server uses TPL to schedule the work on the work items. 每个服务器使用TPL安排工作项的工作。 To truly load balance the work I need to be able to determine how much each server has work items pending. 为了真正实现工作的负载平衡,我需要能够确定每台服务器有多少待处理的工作项。

All I need is to get the number of scheduled work items for a default TaskScheduler. 我需要做的就是获取默认TaskScheduler的计划工作项目数。 Ideally I could to use TaskSchduler.GetScheduleTasks but this method is protected. 理想情况下,我可以使用TaskSchduler.GetScheduleTasks,但此方法受保护。

An obvious solution is to write my own Custom Scheduler and expose number of scheduled tasks. 一个明显的解决方案是编写我自己的“定制计划程序”并公开许多计划任务。 This is far from ideal solution since all I need is this number and realistically I won't write as good TaskScheduler as the default one. 这远不是理想的解决方案,因为我只需要这个数字,实际上我不会写像默认的TaskScheduler一样好的代码。 I can't find the implementation of the Default task Scheduler. 我找不到默认任务计划程序的实现。 If I could than maybe I could inherit from it? 如果我能从中继承呢? Any ideas how I can solve this? 有什么想法可以解决这个问题吗?

All I need is to get the number of scheduled work items for a default TaskScheduler. 我需要做的就是获取默认TaskScheduler的计划工作项目数。

Just add a counter, increment it when a new task is scheduled, decrement it when a task is finished. 只需添加一个计数器,在计划新任务时将其递增,在任务完成时将其递减。 Don't forget to use make it thread safe, eg by using Interlocked.Increment . 不要忘记使用使其线程安全,例如通过使用Interlocked.Increment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM