2013-07-30 112 views
0

我在通過組合多個位圖部件來構建單個位圖並返回一個任務的方法中使用了一些Async方法。Async導致參數錯誤無效

一切工作正常單位我開始添加異步方法和方法,返回任務。

我認爲這個錯誤告訴我兩個線程試圖訪問相同的資源,但我不知道,因爲我不是Async的主人。

這裏是我的XML錯誤:

<Error> 
<Message>An error has occurred.</Message> 
<ExceptionMessage>Parameter is not valid.</ExceptionMessage> 
<ExceptionType>System.ArgumentException</ExceptionType> 
<StackTrace> 
at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat) at System.Drawing.Graphics.MeasureString(String text, Font font) at CoverPage.CoverPage.<>c__DisplayClass1a.<GetBoxContainer>b__19() in c:\Users\Administrator\Documents\Visual Studio 11\Projects\StorefrontSystem\CoverPage\CoverPage.cs:line 219 at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at CoverPage.CoverPage.<>c__DisplayClass5.<>c__DisplayClassb.<<GetCoverPageAsync>b__2>d__10.MoveNext() in c:\Users\Administrator\Documents\Visual Studio 11\Projects\StorefrontSystem\CoverPage\CoverPage.cs:line 165 
</StackTrace> 
</Error> 

我恨張貼了一堆代碼,但我不知道該怎麼做,因爲他們是少數嵌套異步方法可能我得到這個錯誤的原因。

public static Task<Bitmap> GetCoverPageAsync(IProjectInfo projectInfo, IElevation elevation) 
    { 
     return Task.Run(() => 
      { 
       int height = DrawingOptions.PAGE_WIDTH_SIZE, 
        width = DrawingOptions.PAGE_HEIGHT_SIZE, 
        prevBottom = 0; 

       short center = (short)(width/2), 
         startHeight = 50, 
         interiorBorderRight = (short)(width - 40 - 400), 
         interiorLeftBorder = 220, 
         margin = 20; 

       center -= 100; 

       string genTxt; 
       System.Drawing.Size txtSize; 

       Bitmap coverPage = new Bitmap(width, height); 
       coverPage.SetResolution(150, 150); 

       StringBuilder sb = new StringBuilder(); 
       StringFormat stringFormat = new StringFormat(); 
       stringFormat.Alignment = StringAlignment.Center; 
       stringFormat.LineAlignment = StringAlignment.Center; 

       RectangleF rContent = new RectangleF(); 

       using (var dc = Graphics.FromImage(coverPage)) 
       { 
        dc.Clear(DrawingBase.BACK_COLOR); 

        using (var pen = DrawingOptions.GetDetailsPen(PenAlignment.Inset)) 
        { 
         using (var sFont = DrawingOptions.FONT_SMALL) 
         using (var mFont = DrawingOptions.FONT_MEDIUM) 
         using (var lFont = DrawingOptions.FONT_LARGE) 
         { 
          //............................inner info.................................. 
          //............................project location/contact.................. 
          //shop drawings provided for 
          genTxt = "SHOP DRAWINGS PROVIDED FOR:"; 
          txtSize = TextRenderer.MeasureText(genTxt, sFont); 
          prevBottom = startHeight + txtSize.Height; 
          dc.DrawString(genTxt, sFont, new SolidBrush(DrawingBase.LINE_COLOR), center - (txtSize.Width/2) + margin, startHeight); 
          prevBottom += 25; 
          //seperator border 
          dc.DrawRectangle(pen, interiorLeftBorder, prevBottom, interiorBorderRight, 5); 


          //............................project info....................... 
          rContent = new RectangleF(interiorLeftBorder, prevBottom, interiorBorderRight, 300); 
          //dc.DrawRectangle(pen, recProjectContent.X, recProjectContent.Y, recProjectContent.Width, recProjectContent.Height); 

          sb.Append("\n\n").Append(projectInfo.ProjStreet).Append("\n").Append(projectInfo.ProjCity).Append(",") 
           .Append(projectInfo.ProjSt).Append(" ").Append(projectInfo.ProjZip).Append("\n").Append(projectInfo.ProjPhone) 
           .Append(" or ").Append(projectInfo.ProjEmail); 

          // Draw the text and the surrounding rectangle. 
          dc.DrawString(sb.ToString(), mFont, new SolidBrush(DrawingOptions.LINE_COLOR), rContent, stringFormat); 

          //Project name 
          genTxt = projectInfo.ProjectName; 
          txtSize = TextRenderer.MeasureText(genTxt, lFont); 
          dc.DrawString(genTxt, lFont, new SolidBrush(DrawingBase.LINE_COLOR), center - (txtSize.Width/2), prevBottom + 25); 
          //............................project info end.................... 

          //seperator border 
          dc.DrawRectangle(pen, interiorLeftBorder, rContent.Bottom, interiorBorderRight, 5); 


          //............................project installer................................. 
          //dc.DrawRectangle(pen, recInstallerContent.X, recInstallerContent.Y, recInstallerContent.Width, recInstallerContent.Height); 
          rContent = new RectangleF(interiorLeftBorder, rContent.Bottom, interiorBorderRight, 400); 

          sb.Clear(); 
          sb.Append("Installer\n\n\n").Append(projectInfo.Street).Append("\n").Append(projectInfo.City).Append(", ") 
           .Append(projectInfo.StateCD).Append(" ").Append(projectInfo.ZipCode).Append("\n").Append(projectInfo.Phone) 
           .Append(" or ").Append(projectInfo.Email); 
          // Draw the text and the surrounding rectangle. 
          dc.DrawString(sb.ToString(), mFont, new SolidBrush(DrawingOptions.LINE_COLOR), rContent, stringFormat); 
          //installer name 
          genTxt = projectInfo.Name; 
          txtSize = TextRenderer.MeasureText(genTxt, lFont); 
          dc.DrawString(genTxt, lFont, new SolidBrush(DrawingBase.LINE_COLOR), center - (txtSize.Width/2), rContent.Bottom + 85); 
          //............................project installer end.............................. 

          //seperator bordre 
          dc.DrawRectangle(pen, interiorLeftBorder, rContent.Bottom, interiorBorderRight, 5); 
          //............................project installer end.............................. 

          //............................alum cloud................................. 
          //TM 
          dc.DrawImageUnscaled(Resources.tm_200x142, center - (Resources.tm_200x142.Width/2), (int)rContent.Bottom + 25); 
          prevBottom += 100 + (int)rContent.Bottom + Resources.tm_200x142.Height; 

          rContent = new RectangleF(interiorLeftBorder, rContent.Bottom, interiorBorderRight, 450); 
          //dc.DrawRectangle(pen, recACContent.X, recACContent.Y, recACContent.Width, recACContent.Height); 
          // Draw the text and the surrounding rectangle. 
          dc.DrawString("\n\n\n\nThe industry leader in CAD drafting software\nutilizing \"cloud computing\"\ntechnology.", 
               mFont, new SolidBrush(DrawingOptions.LINE_COLOR), rContent, stringFormat); 
          //............................alum cloud end.............................. 

          //............................inner info end.............................. 


          //............................top info boxes.............................. 
          //............................project framing system....................... 
          sb.Clear(); 
          genTxt = "Doors: 0"; 
          if (projectInfo.ProjectLeafs != null) 
          { 
           var leafs = projectInfo.ProjectLeafs.ToList<IProjectLeafs>(); 
           if (leafs.Count() > 0) 
           { 
            sb.Append("Doors: "); 
            for (short i = 0; i < leafs.Count(); i++) 
            { 
             sb.Append(leafs[i].Total + " " + leafs[i].Stile + ", "); 
            } 
            genTxt = sb.ToString().TrimEnd(new char[] { ',', ' ' }) + " Stile"; 
           } 
          } 

          sb.Clear(); 
          sb.AppendLine("Framing: " + elevation.FrameName.Replace("|Storefront", "").Replace("Series", "")) 
           .AppendLine("Elevations: " + projectInfo.Elevations) 
           .AppendLine(genTxt) 
           .AppendLine("Material: " + "6063-T6 Aluminum") 
           .Append("Finish: " + elevation.Bays[0].FinishName + " Anodized"); 

          //product info, top left 
          using (var canvasProdInfo = Task.Run(async() => await GetBoxContainer(610, 150, "Products", sb.ToString(), RotateFlipType.Rotate90FlipNone)).Result) 
          { 
           dc.DrawImageUnscaled(canvasProdInfo,/*left to right*/ margin + 2,/*up down*/ margin); 
          } 
          //............................project framing system end.............................. 

          //notes, bottom left 
          //if (true/*project has a submittal date*/) 
          // { 
          //submitDate = " Original show drawings submittal on July 29, 2013."; 
          //} 
          // else 
          // { 
          genTxt = "1.) Please verify building codes.\n2.) Drawings have not been submitted for review."; 
          //} 
          using (var generalNotes = Task.Run(async() => await GetBoxContainer(610, 150, "General Notes", genTxt, RotateFlipType.Rotate90FlipNone)).Result) 
          { 
           dc.DrawImageUnscaled(generalNotes,/*left to right*/ margin + 2,/*up down*/ (coverPage.Height - generalNotes.Height) - margin + 2); 
          } 
          //............................top info boxes end........................... 


          //............................bottom info boxes............................. 
          //revisions, bottom right 
          using (var revisions = Task.Run(async() => await GetRevisions(610, 150)).Result) 
          { 
           dc.DrawImageUnscaled(revisions,/*left to right*/ coverPage.Width - 170 - margin,/*up down*/ (coverPage.Height - revisions.Height) - margin + 3); 
          } 

          //Drafting, top right 
          using (var draftNote = Task.Run(async() => await GetBoxContainer(610, 150, "Drafting Notes", "1.) " + projectInfo.Note, RotateFlipType.Rotate270FlipNone)).Result) 
          { 
           dc.DrawImageUnscaled(draftNote, /*left to right*/ coverPage.Width - 170 - margin,/*up down*/ margin - 1); 
          } 
          //............................bottom info boxes end.......................... 


          //bottom line 
          dc.DrawLine(pen, coverPage.Width - 200, margin, coverPage.Width - 200, coverPage.Height - margin); 
          //top line 
          dc.DrawLine(pen, 200, margin, 200, coverPage.Height - margin); 
          //page edge 
          dc.DrawRectangle(pen, margin, margin, width - (margin * 2), height - (margin * 2)); 
         }; 
        }; 
       }; 

       coverPage.RotateFlip(RotateFlipType.Rotate90FlipNone); 
       return coverPage; 
      }); 
    } 

有什麼建議嗎?

非常感謝這個!

回答

0

我發現我的問題。

我正在以靜態方式訪問我的字體,如在常量中,並在內部使用(){}塊。

因此,我寫了一個函數,返回一個新的常量字體副本,這種方式沒關係,如果字體被處置。

0

我相信你傳遞給DrawString方法的一些參數對它無效。

這可能是:

  • 空字符串
  • 處置對象,如字體或刷子
相關問題