Marino Šimić
Marino Šimić

Reputation: 7342

WPF to Silverlight threading conversion

In WPF on .Net 4 i had a similar loop spawning background work:

        Point[] points = GetPoints();
        List<Task> tasks = new List<Task>();

        // loop bitmap
        for (int x = 0; x < bitmap.Width; x++) {
           for (int y = 0; y < bitmap.Height; y++) {
              Task t = new Task((object point) => {
                  Point p = points[((Point)point).Y * bitmap.Width + ((Point)point).X];
                  p.CalculateInterference(); // a bit slow
              }, new Point(x, y));
              t.Start();
              tasks.Add(t);
           }
           // after spawning tasks for all the Y column I need them to finish before proceeding
           foreach (var t in tasks) {
             t.Wait();
           }
           tasks.Clear();
        }

This was easy with the Task class in .NET4, but on Silverlight 3 I don't see them...

What would be the easiest but still correct way to make this work in Silverlight without spawning Y threads in parallel, because Y can be a large value. It would be good it it uses the available CPU resources of the host...

Thanks!

Upvotes: 1

Views: 205

Answers (1)

Rick Sladkey
Rick Sladkey

Reputation: 34250

For the .NET4 version you would be better off using Parallel.For:

It is exactly for this kind of situation.

For Silverlight you can grow your own and here is some code to get you started:

See the section entitled Loop Tiling.

Upvotes: 3

Related Questions