none
Task 返回值如何赋值给前台控件 RRS feed

  • 问题

  • HeatMapMaker类的MakeHeatMap()返回值是Task<Bitmap>类型,如何把这个返回值赋给前台的pictureBox2对象?

    直接这样赋值通不过

    var hmMaker = new HeatMapMaker
                {
                  ...           

                };

    this.pictureBox2.BackgroundImage = hmMaker.MakeHeatMap(); //这里报错

    public class HeatMapMaker
        {
            ...

            public Task<Bitmap> MakeHeatMap()
            {
                return Task.Factory.StartNew(()=>
                {
                    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);
                });         
            }

          ...

        

    2013年5月13日 9:10

答案

全部回复

  • 你或许应该考虑使用4.5中的新特性——异步:

    public async Task<Bitmap> MakeHeatMap()
            {
                Bitmap bt = new Bitmap(12,12);
                await Task.Factory.StartNew(() =>
                { 
                    //Deal with bt……
                });
                return bt;
            }

    然后:

    var r = await MakeHeatMap();

    最后把r赋值给PictureBox的BackGroundImage属性。注意调用MakeheatMap的时候所在

    的那个方法必须也加async!

       

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年5月13日 9:25
    版主
  • async await 需要.net 4.5环境。

    在4.0环境中运行。

    有其他办法吗?

    2013年5月14日 7:33
  •  

    publicstaticTask<Bitmap>MakeHeatMap()         {             Bitmapbt=newBitmap(12,12);           return Task<BitMap>.Factory.StartNew(()=>bt……);         }

    PictureBox p = new PictureBox(); p.Image = MakeHeatMap().Result;

            

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年5月14日 7:46
    版主
  • 你可以调用 Result属性来获得Bitmap试试的,并且在.NEt 4.0 下可以使用async和await的,但是你必须安装SP1补丁,具体步骤可以查看下面的博客:

    如何在VS2010中使用Async功能?


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. my sample

    • 已标记为答案 VCResearch 2013年5月30日 1:04
    2013年5月22日 3:25