I faced a very similar situation with yours. I ended up using my log data to tell me about how many threads were used per minute. This gave me an approximate number, but not an exact one.
I do not believe that TPL can give you telemetry when using streams. If you want to implement something more precise, I would recommend that you implement the logic in each task / thread in order to mark some kind of general list of start time and its completion time. Here is an example of how I approach him to get started.
public class DoSomeTPLWork { public static void Start() { List<int> numberList = Enumerable.Range(1, 1000).ToList(); Parallel.ForEach(numberList, number => { ThreadTracking.ThreadStarted(); int square = number * number; Console.WriteLine("Square of {0} is {1}", number, square); ThreadTracking.ThreadFinished(); } ); var threadInfo = ThreadTracking.GetThreadInfo(); } } public class ThreadTracking { private static ConcurrentBag<ThreadInfo> _threadInfo = new ConcurrentBag<ThreadInfo>(); public static void ThreadStarted() { var threadInfo = new ThreadInfo(Thread.CurrentThread.ManagedThreadId); threadInfo.Start(); _threadInfo.Add(threadInfo); } public static void ThreadFinished() { var threadInfo = _threadInfo.Where(ti => ti.ThreadId == Thread.CurrentThread.ManagedThreadId && !ti.Complete).SingleOrDefault(); if(threadInfo != null) { threadInfo.Stop(); } } public static List<ThreadInfo> GetThreadInfo() { return _threadInfo.ToList(); } } public class ThreadInfo { public bool Complete { get; set; } public int ThreadId { get; set; } public DateTime? TimeStarted { get; set; } public DateTime? TimeFinished { get; set; } public ThreadInfo(int threadId) { ThreadId = threadId; } public void Start() { TimeStarted = DateTime.Now; Complete = false; } public void Stop() { TimeFinished = DateTime.Now; Complete = true; } }
Using the data, you can see how many threads are being used for any second, adding a few more methods to query the data or simply pasting them into Excel to play with it.
Adamv source share