public static Task TraceScene( Camera camera, World world, DrawPixel drawPixel, int maximumReflections, CancellationToken cancelToken) => Task.Factory.StartNew(() => { try { Parallel.ForEach( Shuffled(GetAllPoints(camera.Dimensions)), new ParallelOptions { CancellationToken = cancelToken, }, (position, loopState) => { var ray = camera.CreateRayForPixel(position.X, position.Y); var color = world.ComputeColor(ray, maximumReflections); drawPixel(position.X, position.Y, color); }); } catch (OperationCanceledException) { // Why does this throw??? What am I supposed to do here other than ignore it??? } });
public static Task TraceSceneByRows( Camera camera, World world, DrawPixel drawPixel, int maximumReflections, CancellationToken cancelToken, Action reportRowRendered) => Task.Factory.StartNew(() => Parallel.For( 0, camera.Dimensions.Height, new ParallelOptions { CancellationToken = cancelToken }, row => { for (int col = 0; col < camera.Dimensions.Width; col++) { var ray = camera.CreateRayForPixel(col, row); var color = world.ComputeColor(ray, maximumReflections); drawPixel(col, row, color); } reportRowRendered(); }));