I'm trying to print document pages using System.Drawing.Printing.PrintDocument OnPrintPage method.
I need to call and external service to get measurements needed to print current page. When I call the API, graphics objects seems to be locked because of multithreading(?) and throws exception when I try to use it after acquiring the data from API. Can't get the data before printing the document. Is there any way to avoid such blocking?
protected override async void OnPrintPage(PrintPageEventArgs e)
{
var location = await foo.CallWebAPI();
//Do something with Graphics
e.Graphics.DrawLine(....);
//ERROR - Argument Exception
}
EDIT:
I forgot to add async in signature, but that's not part of my question. Graphics object in debugger seems to be locked/disposed after awaited call. It shows ArgumentException on any of graphics fields. Managed to solve it by using Result instead of awaiting call, but I wonder if there is any better solution.
var location = foo.CallWebAPI().Result;