2013-07-01 61 views
0

我正在從多個位圖中構建一個位圖。我所做的就是調用返回任務的多個方法。如何使用.ContinueWith調用異步任務

我遇到的問題是死鎖,因爲我試圖從.Result的.ContinueWith()中的異步方法中檢索值,並且這不起作用,因爲它會變成異步方法int同步方法,這導致我陷入僵局。

我的問題是我怎麼叫.ContinueWith裏面的異步方法,和或什麼是實現這種類型的方法的正確方法?

我需要刪除。結果,正確地調用異步方法任務。什麼是正確的方法?

這裏是我的代碼。

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; 
       } 
      } 
     } 
    }; 
} 

回答

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; 
    } 
} 
+0

謝謝斯蒂芬·克利裏 –

+0

我想在結束我的異步方法與異步擴展。如果我要返回任務怎麼辦?該方法需要和異步? –

+0

是的,它應該以'Async'結尾。 –