This example solution uses the same approach used by the preceding solution. It uses form-level variables to pass information to the parallel instances of a method. Those instances perform calculations and then use locking to save their results to the form-level variables.
The following code shows the form-level variables:
private int NumHits = 0;private object LockObject = new object();private int MonteCarloWidth = 0, MonteCarloHeight = 0;private Bitmap MonteCarloBitmap = null;private const int PointsPerCall = 10000;
The following method starts the parallel execution:
// Use Monte Carlo simulation to estimate pi.private double MonteCarloPi(int numPoints){ // Make a bitmap to show points. MonteCarloWidth = pointsPictureBox.ClientSize.Width; ...