Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

DynaPDF Manual - Page 42

Previous Page 41   Index   Next Page 43

Language Bindings
Page 42 of 750
However, the DLL is unloaded each time if the reference count of the DLL is zero. In most cases
it makes sense to hold one instance of the wrapper class in memory to avoid unloading the
library. The internal resources used by DynaPDF are always freed when CloseFile() is called
(except when the file is created in memory), so that there is no need to destroy the main instance
of TPDF.
Exception handling in Delphi
DynaPDF itself uses no native Delphi exception handling. Error messages and warnings are
passed to an error callback function if any (see SetOnErrorProc()). If no callback function is
used, then use the function GetErrorMessage() to get information about the last error.
However, the wrapper class TPDF uses native exceptions in the following cases:
When creating a new instance of the wrapper class TPDF.
When loading a DLL function with the API function GetProcAddress() (all functions).
If a function listed above fails, then an exception is raised by the class TPDF. Always
encapsulate all function calls into a try / except block. Only a few exceptions can occur but these
exceptions must not be ignored. Especially when using DynaPDF in multi-threading
applications it is highly recommended to use try / except or try / finally blocks. A thread must
always catch all exceptions inside the thread.
Using DynaPDF in Multithreading Applications
The usage of DynaPDF inside a thread is the same as in single-threaded applications.
However, if a callback function should be used, you must make sure that the callback function
is declared in the same thread or that each thread uses its own copy of the callback function. In
addition, it is highly recommended that only thread-safe functions are called inside the callback
function. If any unsafe function must be executed the function that causes the execution of the
callback function must be synchronized because it is impossible to synchronize a callback
function itself.
Threads should be used completely isolated from the main-thread of the application. Function
calls to and from the main-thread must be synchronized. The entire PDF file should be created
inside the thread including the instance of the wrapper class TPDF. The class instance must also
be deleted before the thread is terminated.
A running thread can be terminated at any time but it is highly recommended to wait for any
running functions to end before a thread will be terminated. This can be done easily by
checking the property Terminated within the thread before a new function is executed.
After a running function returns, the class instance can be destroyed by using the Free() method
for that instance. This will clean up the used resources and the thread can be terminated. The
instance of the wrapper class TPDF can be safely destroyed at any time after a running function
 

Previous topic: General Usage

Next topic: Changing the configuration scripts