Print Page | Close Window

Multi-Threading from C#

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: General Discussion
Forum Description: Discussion board for Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3487
Printed Date: 26 Nov 24 at 4:59AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Multi-Threading from C#
Posted By: davidgm
Subject: Multi-Threading from C#
Date Posted: 28 Jul 17 at 5:32PM
Hello,

I am facing several issues using threads in C# with this library.

I know that the library is not multi-thread safe, but i also did read this:

"However, Debenu Quick PDF Library can be used in a multi-threaded environment so long as a separate instance of the library is created by each thread."

I create different threads in my C#, and i init the library this way in every thread:

        public Foxit(string KEY)
        {
            // Different filename for 32-bit and 64-bit DLL

            string DLLprefix = "DebenuPDFLibraryDLL";
            string DLL64prefix = "DebenuPDFLibrary64DLL";

            string dllName;

            // Check to see if IntPtr size is 4.
            // If 4 then it's 32-bit, if 8 then
            // it is 64-bit.
            if (IntPtr.Size == 4)
            {
                dllName = DLLprefix + VERSION.ToString("D4") + ".DLL"; // 32 bits
            }
            else
            {
                dllName = DLL64prefix + VERSION.ToString("D4") + ".DLL"; // 64 bits
            }

            // Load the library
            DPL = new PDFLibrary(dllName);

            // Unlock the library
            m_started = DPL.UnlockKey(KEY) == 1;
        }

However, when several threads are using the library, i get the following exception:

Excepcion: System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
   in DebenuPDFLibraryDLL1312.PDFLibrary.PrintDocument(String PrinterName, Int32 StartPage, Int32 EndPage, Int32 Options)

The method causing the exception is PrintDocument, and this is what i'm using from the lib.

I solved the problem temporarily by creating a little executable that does the printing part only. I do a shell exec from each thread to this executable. It is not optimal, but it is working properly, and i get the multi-core boost that i need for my application. Speed is crucial for my project.

Am i missing any special initialization for a muti-threaded C# application ?

Thank you very much.



Replies:
Posted By: tfrost
Date Posted: 31 Jul 17 at 9:14AM
Surely it is more likely that the printing operation is the part that does not support multi-threading?  It could be an issue with the printer driver.  Are both threads attempting to write to the same physical printer? Is spooling enabled and working for the printer port?  Google tells me that 0x80004005 can arise when sharing a printer, but it is hard to find a definitive meaning for this code. 

A simpler solution than using a separate program might be to enclose the call to PrintDocument inside a critical section, especially if printing to the same printer is likely.


Posted By: davidgm
Date Posted: 01 Aug 17 at 11:50AM
Thanks for the reply.

I tested 4 different threads, each one printing to a different printer, and each printer from a diferent manufacturer with a different driver. It failed.

I tested with spooling enabled & disabled, and with the spooler set to print immediately and set to print after last page has been spooled. Everything failed.

Print processor was set to winprint & raw in all the cases.

If you use a lock() with a critical section before the PrintDocument, it works ofc, but you lose all the multi-core boost this way.

So far, the best solution for multi-core printing is a little helper (print.exe) that runs in a separate process. This way it works, even sending many jobs to the same printer at the same time.

I have been testing many different libraries from different companies, and this library is the best by far in terms of printing speed.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk