我正在使用几个位图构建一个位图。我所做的是调用多个返回任务的方法。
我遇到的问题是死锁,因为我试图通过.Result从.ContinueWith()中的异步方法中检索值,这不起作用,因为它变成了异步方法int一个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;
}
}
}
};
}
答案 0 :(得分:5)
您认为需要什么ContinueWith
?
Result
的相应替换为await
。作为旁注,您应该使用“异步”结束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;
}
}