C# API Example has events bug, InvalidOperationException

PDF-XChange Drivers API (only) V4/V5
This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-XChange Printer Drivers SDK (only) - VERSION 4 & 5 - Please use the PDF-Tools SDK Forum for Library DLL assistance.

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Stefan - PDF-XChange

bkwdesign
User
Posts: 1
Joined: Thu May 10, 2012 8:28 pm

C# API Example has events bug, InvalidOperationException

Post by bkwdesign »

I just built the C# project from the PDF-XChange 4 API that I freshly downloaded today.
FWIW, I'm still on WinXP SP3, dual core

When I run it, the printing functionality technically works just fine, but the big textbox on the form never registers any of the events. Specifically, I'm talking about this form:

\PDF-XChange 4 API\Examples\APIExamples\C#Examples\PDFdriverAPI\Form1.cs

If you look at line 27, the method for writing to the text box, it looks like this:

Code: Select all

        void AddEventLog(string sText)
        {
            tbEventsLog.Text = sText + "\r\n" + tbEventsLog.Text;
            if (bPXCPrinterDefault)
            {
                PDFPrinter.RestoreDefaultPrinter();
                bPXCPrinterDefault = false;
                tbEventsLog.Text = "!Restored Default Printer\r\n" + tbEventsLog.Text;
            }
        }
And an example event method looks like this, which is fine:

Code: Select all

        void prn_OnStartPage(int JobID, int nPageNumber)
        {
            AddEventLog("StartPageEvent");
        }
However, because the events are being raised by a different thread, they are not allowed to update controls on the form in this sample windows application. See this MSDN referenencefor more info on why and how to handle.

My fix is to replace the AddEventLog method that starts on line 27, with a block of code that looks like this (don't forget the delegate at the end of this snippet):

Code: Select all

        private void AddEventLog(string sText)
        {
            if (this.InvokeRequired)
            {
                AddEventLogCallback d = new AddEventLogCallback(AddEventLog);
                this.Invoke(d, new object[] { sText });
            }
            else
            {
                tbEventsLog.Text = sText + Environment.NewLine + tbEventsLog.Text;
                if (bPXCPrinterDefault)
                {
                    PDFPrinter.RestoreDefaultPrinter();
                    bPXCPrinterDefault = false;
                    tbEventsLog.Text = "!Restored Default Printer\r\n" + tbEventsLog.Text;
                }
            }
        }

        private delegate void AddEventLogCallback(string txt);

User avatar
John - Tracker Supp
Site Admin
Posts: 5225
Joined: Tue Jun 29, 2004 10:34 am

Re: C# API Example has events bug, InvalidOperationException

Post by John - Tracker Supp »

Many thanks for the info - we will advise our project team of this ommission accordingly and ask them to check the sample app for this issue and correct as necessary.
If posting files to this forum - you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded - thank you.

Best regards
Tracker Support
http://www.tracker-software.com