c# - Processing a ManagementObjectCollection using Parallel.ForEach(...) -
i'm using following code process items in managementobjectcollection in parallel:
using (managementobjectcollection results = this._searcher.get()) { // type arguments method parallel.foreach<tsource>(...) cannot inferred usage parallel.foreach(results, (mo, loopstate) => { // process mo }); }
if specify type, complains this
using (managementobjectcollection results = this._searcher.get()) { // cannot convert managementobjectcollection [..].ienumereable<managementobject> parallel.foreach<managementobject>(results, (mo, loopstate) => { // process mo }); }
how can make work properly? , why second code block not work (afaics, managementobjectcollection implements ienumerable why complaining)?
it implements non-generic ienumerable
interface, hence making ienumerable<object>
, not ienumerable<managementobject>
.
i'd suggest casting using cast<t>
cast each element iterate:
parallel.foreach(results.cast<managementobject>(), (mo, loopstate) => { // process mo });
if reason need typed list, can cast before:
var managementobjects = results.cast<managementobject>().tolist(); parallel.foreach(managementobjects, (mo, loopstate) => { // process mo });
Comments
Post a Comment