0

Is there a way to get the size (ie. the number of elements) of TOmniBlockingCollection of OmniThreadLibrary?

I checked the code, my initial conclusion is no, but I'm not sure.

mghie
  • 32,028
  • 6
  • 87
  • 129
Edwin Yip
  • 4,089
  • 4
  • 40
  • 86
  • 1
    What would you do with that information? – David Heffernan Jun 10 '14 at 06:10
  • @DavidHeffernan, I spawn multiple threads (5 by default) to handle elements in a `TOmniBlockingCollection` but sometimes the collection has less elements than 5, thus I need that size info to avoid spawning unnecessary threads. Anyways, I've get around this limit by determining the size of my elements before it's put into `TOmniBlockingCollection`. – Edwin Yip Jun 10 '14 at 06:20
  • Why can't you remember how many items you put in? – David Heffernan Jun 10 '14 at 07:25
  • You would be better off using a Parallel.ForEach on the TOmniBlockingCollection to create the processing threads. Gabr has a nice example here (http://www.thedelphigeek.com/2010/02/three-steps-to-blocking-collection-3.html). If you really have to do try and get the count from the collection then the only way I could think of is to use the enumerator to 'iterate' through the list and then add them to a new collection. – Graymatter Jun 10 '14 at 07:54
  • @Graymatter Then you might over subscribe – David Heffernan Jun 10 '14 at 08:00
  • Why downvote without any explanation? @DavidHeffernan, of course I can remember how many items. TList and TStrings has the count property, why? Imagine in parentFunc() I have the item count, then the collection passed to childFunc() where I'll not have the item count unless I add a formal param to pass it into childFunc(). It's more about **convenience** and **clean code** but not a tech problem I couldn't get around. – Edwin Yip Jun 10 '14 at 08:01
  • @Graymatter, no, each loop of Parallel.ForEach() involves restarting my thread. Here I'm not mostly concerning the performance of restarting the thread since it's pooled in the global OTL pool, but the state of the thread execution, eg, I don't want to recreate an Internet connection in each loop of the collection. – Edwin Yip Jun 10 '14 at 08:05
  • I don't understand the downvote either but the policy here is to avoid voting chatter in the question itself. So I reverted that update. – David Heffernan Jun 10 '14 at 08:27

1 Answers1

0

Is there a way to get the size (ie. the number of elements) of TOmniBlockingCollection of OmniThreadLibrary?

No there is not.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490