TN01 : Multitasking AMYUNI PDF Converter [Updated]

This forum contains a list of all our Technical Notes. These provide developers with sample applications and information on using our products.
Post Reply
Posts: 119
Joined: Fri Oct 14 2005
Location: Montreal

TN01 : Multitasking AMYUNI PDF Converter [Updated]

Post by Devteam »

TN01: Using the Amyuni PDF Converter in Multi-threaded Environments

When using the Amyuni PDF Converter printer driver in multi threaded environments, one common
situation that a developer should be aware of is the possibility of multiple users or threads
simultaneously accessing the printer driver.

The Amyuni PDF Converter exposes a number of API calls that can be used to correctly handle this

The Lock()\Unlock() methods can be used in multi-threading situations to avoid conflicts between
multiple applications or multiple threads requesting simultaneous access the Converter products.
The Amyuni PDF Converter printer library uses the system registry to interact with the printer driver.
When the Lock() function is used, the output file name and filename options are set using the
SetDocFileProps() method and not from the SetDefaultFileName() and SetFileNameOptions() methods.
When the SetDefaultFileName() and SetFileNameOptions() methods are used, these methods read and
write registry entries that are common to all users.

The Lock() method requires the developer to pass the method the document title of the document that
is going to be printed. This is same document title that will appear in the print spooler.
Note: When implementing the Locking mechanism one way to quickly check how your printing
application is generating document titles is to first pause the PDF Converter and then print to it. In the
print spooler you will see the document name to use.

What the Lock() method actually does is reserve a private area where the settings for that specific job
are set. SetDocFileProps sets the preferences for the print job and when the job is being printed, the
spooler gets the settings for that job and then deletes that private place.

The Unlock method is used after printing has ended to make sure another printout can start. The call to
Unlock is needed only in the case where an error occurs; the printer will otherwise call Unlock internally
as soon as printing starts to allow another printout to occur in parallel. The Timeout value represents
the Timeout in milliseconds after which the function returns in case of an error.

Technical Note TN01 is a sample project that illustrates how to implement the Lock()\Unlock() methods.
The project contains an application that spawns a number of worker threads in a loop and each of these
worker threads simultaneously print to the Amyuni PDF Converter.

The main printing process occurs in this function:

Code: Select all

/// <summary>
/// This method is executed by each thread
/// </summary>
static public void thread_printing()
    //Create a CDIntfEx object
    CDIntfEx.CDIntfEx PDF = new CDIntfEx.CDIntfEx();
    //Get handle to installed printer

    //Need to use distinct document title
    //this example is using a timestamp to append to the PDF file name
    string docTitle = "amyuni_" + DateTime.UtcNow.Millisecond.ToString();

    //Create a PrintDocument object
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

    //When ever possible, we suggest setting the application’s active printer
    //instead of changing the system’s default printer.
    pd.PrinterSettings.PrinterName = PDFprinter;
    //set document title that was created above
    pd.DocumentName = docTitle;
    //Lock document title

    //Set Amyuni PDF Converter printer settings
    PDF.SetDocFileProps(docTitle, 3, "", "c:\\temp\\" + docTitle + ".pdf");

    //EnablePrinter needs to be called before every print job
    //If this call is not done before each print job, the printer
    //will time-out and generate a printer not activated error message
    PDF.EnablePrinter(strLicenseTo, strActivationCode);

    //Print something

    //Unlock() only gets called internally if the PDF Converter
    //is unable to generate a PDF document
    PDF.Unlock(docTitle, 1000);
You can download the full TN01 from the following link:

You can also find additional information in our online documentation at: ... s/Lock.htm
Post Reply