WPF Silverlight的线程转换 [英] WPF to Silverlight threading conversion
问题描述
在WPF在.NET 4中我有一个类似环产卵后台工作:
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();
}
这是很容易与.NET4任务类,但在Silverlight 3中我没有看到他们......
This was easy with the Task class in .NET4, but on Silverlight 3 I don't see them...
会是什么,使这项工作在Silverlight没有产卵Ÿ线程并行,因为Y可以是一个较大的值最简单的,但仍然是正确的方式。这将是良好的IT它所使用的主机的可用CPU资源...
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...
谢谢!
推荐答案
对于.NET4版本,你会使用会更好的Parallel.For
:
For the .NET4 version you would be better off using Parallel.For
:
正是针对这种情况。
有关Silverlight中,你可以增加自己的,这里是一些code,让你开始:
For Silverlight you can grow your own and here is some code to get you started:
请参阅部分标题为循环平铺
这篇关于WPF Silverlight的线程转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!