Hello I keep getting the following error, and can't figure out a solution. I've looked at similar posts, but no help.
Managed Debugging Assistant
'ContextSwitchDeadlock'
: 'The CLR has been unable to transition from COM context0xff3c60
to COM context0xff3d18
for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such asCoWaitForMultipleHandles
) and routinely pump messages during long running operations.
I'm trying to print using the C#
PrintDocument.Print()
Method. My code can be seen below.
This code is in the main method:
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "WOOSIM PORTI-P60";
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
and this is the print page event that gets called with the pd.Print()
call:
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font printFont = new Font("Arial", 10);
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = 2;
float topMargin = 2;
String line = null;
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Iterate over the file, printing each line.
while (count < 10)
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString("test"+count, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
The pd_PrintPage
call does end since line == null
, but it seems like the thread doesn't.