积极答复者
这段代码应该如何改造为.net 4.0能兼容的代码

问题
-
下面代码是4.5的, task.run在4.0中编译不能通过.
请问应该如何改造使其能支持4.0.
另外, () =>{...}这是什么用法,哪里有详细介绍.
谢谢.
public Task<Bitmap> MakeHeatMap()
{
return Task.Run(() =>
{
var result = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);this.GrayMap = this.makeGrayMap().Result; //*****
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
{
var grayVal = this.GrayMap.GetPixel(x, y);
var index = grayVal.A;
var color = ColorUtil.GetColorInRamp(index, this.ColorRamp);
result.SetPixel(x, y, color);
}
}return ColorUtil.AdjustOpacity(result, this.Opacity);
});
}
答案
-
c# 4.0 可以使用Task.Factory.StartNew(...)
参考这里:
http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx
- 已标记为答案 VCResearch 2013年5月13日 8:48
-
hello,
1.Task.Run是.net 4.5的语法,你可改用Task.Start or Task.Factory.Start
2.Lambda是匿名方法的语法糖
http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
委派写法演变历史,從1.0 委托到3.0委派,进化到Lambda
http://huan-lin.blogspot.tw/2009/01/delegate-revisited-csharp-1-to-2-to-3.html
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已编辑 余小章MVP 2013年5月10日 6:19
- 已标记为答案 VCResearch 2013年5月13日 8:48
全部回复
-
c# 4.0 可以使用Task.Factory.StartNew(...)
参考这里:
http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx
- 已标记为答案 VCResearch 2013年5月13日 8:48
-
hello,
1.Task.Run是.net 4.5的语法,你可改用Task.Start or Task.Factory.Start
2.Lambda是匿名方法的语法糖
http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
委派写法演变历史,從1.0 委托到3.0委派,进化到Lambda
http://huan-lin.blogspot.tw/2009/01/delegate-revisited-csharp-1-to-2-to-3.html
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已编辑 余小章MVP 2013年5月10日 6:19
- 已标记为答案 VCResearch 2013年5月13日 8:48