0

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 context 0xff3c60 to COM context 0xff3d18 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 as CoWaitForMultipleHandles) 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.

rene
  • 41,474
  • 78
  • 114
  • 152
Pat
  • 1
  • is this a winform app? .Net Framework 4.x? Is `pd.Print();` called on the UI thread? – rene Apr 20 '21 at 17:05
  • Doesn't need to be an issue: https://stackoverflow.com/questions/578357/visual-studio-contextswitchdeadlock – rene Apr 20 '21 at 17:09
  • Are you sure that code shows the actual problem? Or is your while loop indeed what the comment suggests: the iteration over ALL the lines of one file. If so: how many lines are there in that file? – rene Apr 20 '21 at 17:16
  • @rene To answer your first question, it is a winform app and .Net Framework 4.5 – Pat Apr 20 '21 at 17:17
  • 1
    Are you setting `String line` to something that is not `null` at some point? – Jimi Apr 20 '21 at 17:31
  • @Jimi no I am not. I copied some code from the Microsoft docs and took out the part where line is used. I kept it for the HasMorePages part since I plan on implementing this in the future, but for my test, I had taken it out – Pat Apr 20 '21 at 17:44
  • 1
    Well, then `ev.HasMorePages` is always `true` and you never stop printing. – Jimi Apr 20 '21 at 17:46
  • actually the line != null will always be false since line == null so ev.HasMorePages will always be false – Pat Apr 20 '21 at 17:53
  • Yep, it came out wrong. *Check that line: if `ev.HasMorePages` is always `true` you never stop printing*. -- I don't know whether this is all the code you have. If that's the case, that's not the source of the problem. Did you debug the this app? Does it become unresponsive only after you start printing? How do you start the print process? Are you sure you don't have any recursive procedure? If the Form becomes unresponsive, try `CTRL+ALT+BREAK` and `Debug->Window-Threads` / `Call Stack` etc., to verify where it stops and what's being processed at that time. – Jimi Apr 21 '21 at 00:33

0 Answers0