The .Net framework has built-in support for parallellization. Below is an example of a parallell loop when making growth projections for a collection of stands.
| Example Title |
Copy Code |
|---|---|
var fromDate = new DateTime(2015, 1, 1); var toDate = new DateTime(2020, 1, 1); var resDict = new ConcurrentDictionary<string, ApiState>(); Parallel.For(0, nbStands, i => { var growthCalculator = new GrowthCalculator(controlCategory: null, heightCalibrationWeight: 1.0); var compartment = new ApiCompartment(fromDate, _evenAgedClass[i], _soilMoistCode[i], _vegType[i], _eastCoord, _northCoord, _altitude, _siSpecies[i], _siteIndex[i], GISHelpers.GetSWEREFf99_Projection()); compartMent.Id = _standId[i]; compartment.GrowthCalculator = gc; addTreeLayerData(compartment, i); // Some method that populates a compartment with tree layer data ApiState res = compartment.Grow(fromDate, toDate, growthCalculator); resDict.TryAdd(compartment.Id, res.DeepClone()); // The apiState DeepClone() copies all data, but not any reference to the ApiCompartment }); | |