0

我正在用几个位图构建一个位图。我所做的是调用多个返回任务的方法。

我遇到的问题是死锁,因为我试图通过 .Result 从 .ContinueWith() 中的异步方法中检索值,但这不起作用,因为它将异步方法转换为sync 方法,这导致我陷入僵局。

我的问题是如何调用 .ContinueWith 中的异步方法,或者实现此类方法的正确方法是什么?

我需要删除 .Result 并正确调用异步方法 Task 。正确的方法是什么?

这是我的代码。

 public async static Task<Bitmap> RollUpDrawingsImage(IElevation elevation)
{
    int height = 0, width = 800;
    Bitmap completeDrawings = null;

    using (Bitmap elevationDoor = await ShopDrawing.Merger.MergeElevationAndDoor(elevation, RotateFlipType.Rotate90FlipNone))
    {
        using (Bitmap partsList = await MaterialsList.Manager.GetMaterialList(elevation).ContinueWith(async (ml) => await ml.Result.GetDrawing()).Result)
        {
            using (Bitmap optimized = await Optimization.Manager.GetOptimizedParts(elevation).ContinueWith(async (op) => await op.Result.GetDrawing()).Result)
            {
                using (Bitmap cutSheet = await CutSheet.Manager.GetCutSheet(elevation).ContinueWith(async (cs) => await cs.Result.GetDrawing()).Result)
                {
                    height = (elevationDoor.Height + optimized.Height + cutSheet.Height + partsList.Height);
                    completeDrawings = new Bitmap(width, height + 40);

                    using (var dc = Graphics.FromImage(completeDrawings))
                    {
                        dc.DrawImageUnscaled(elevationDoor, 0, 0);

                        dc.DrawImageUnscaled(partsList, 0, elevationDoor.Height + 10);

                        dc.DrawImageUnscaled(optimized, 0, (elevationDoor.Height + partsList.Height) + 20);

                        dc.DrawImageUnscaled(cutSheet, 0, (elevationDoor.Height + partsList.Height + optimized.Height) + 30);
                    };

                    return completeDrawings;
                }
            }
        }
    };
}
4

1 回答 1

5

是什么让你觉得你需要ContinueWith

的适当替换Resultawait。作为旁注,您应该async以“异步”结束方法:

public async static Task<Bitmap> RollUpDrawingsImage(IElevation elevation)
{
  int height = 0, width = 800;
  Bitmap completeDrawings = null;

  using (Bitmap elevationDoor = await ShopDrawing.Merger.MergeElevationAndDoorAsync(elevation, RotateFlipType.Rotate90FlipNone))
  using (Bitmap partsList = await (await MaterialsList.Manager.GetMaterialListAsync(elevation)).GetDrawingAsync())
  using (Bitmap optimized = await (await Optimization.Manager.GetOptimizedPartsAsync(elevation)).GetDrawingAsync())
  using (Bitmap cutSheet = await (await CutSheet.Manager.GetCutSheetAsync(elevation)).GetDrawingAsync())
  {
    height = (elevationDoor.Height + optimized.Height + cutSheet.Height + partsList.Height);
    completeDrawings = new Bitmap(width, height + 40);

    using (var dc = Graphics.FromImage(completeDrawings))
    {
      dc.DrawImageUnscaled(elevationDoor, 0, 0);
      dc.DrawImageUnscaled(partsList, 0, elevationDoor.Height + 10);
      dc.DrawImageUnscaled(optimized, 0, (elevationDoor.Height + partsList.Height) + 20);
      dc.DrawImageUnscaled(cutSheet, 0, (elevationDoor.Height + partsList.Height + optimized.Height) + 30);
    }

    return completeDrawings;
  }
}
于 2013-07-01T01:45:36.363 回答