导致参数错误的异步无效
本文关键字:异步 无效 错误 参数 | 更新日期: 2023-09-27 18:25:42
我在一个方法中使用了一些Async方法,该方法通过组合多个位图部分来构建单个位图并返回一个Task。
一切都很好,我开始添加异步方法和返回Task的方法。
我认为错误告诉我两个线程正在尝试访问相同的资源,但我不知道,因为我不是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;
});
}
有什么建议吗?
非常感谢这个!!!
我发现了我的问题。
我以一种静态的方式访问我的字体,比如在常量中,并在using(){}块中。
因此,我编写了一个函数,返回常量字体的新副本,这样,字体是否被处理就无关紧要了。
我认为您传递给DrawString
方法的一些参数对它无效。
可能是:
- 空字符串
- 已处理的对象,如字体或画笔