Xojo Conferences

DynaPDF Manual - Page 594

Previous Page 593   Index   Next Page 595

Function Reference
Page 594 of 770
DynaPDF is thread-safe but the condition is that every thread uses its own PDF instance and
anything that should be done must then be done in this thread. When we render pages in a
viewer then we must work with one instance in different threads. This tiny difference is very
important since it is a huge difference whether a library must protect all functions from
competing access or whether it must only isolate its data from one instance to another.
A viewer should only load the page from an external PDF file that should now be rendered. So, don't
call a function like ImportPDFFile() to import the entire file when it is opened. Instead, load pages on
demand as follows:
When the user requests to open a PDF file then open it with OpenImportFile().
Call GetInPageCount() to determine the number of pages in it.
Create an array of pointers that holds the pointers of pages which were already loaded.
Initialize the array with NULL so that it can be used to perform a duplicate check.
Initialize the scroll bars, zoom factor and so on, and create the image buffer so that the first
page can be displayed.
When a page should be displayed then import it first in the main thread if necessary as
if (m_Pages[m_PageNum-1] == NULL)
// Important: Use EditPage() and not Append() to keep the pages
// sorted! Holes are filled with empty pages by EditPage().
pdfEditPage(m_PDF, m_PageNum);
pdfImportPageEx(m_PDF, PageNum, 1.0, 1.0);
// Store the page pointer in the duplicate array
m_Pages[m_PageNum-1] = pdfGetPageObject(m_PDF, m_PageNum);
Inititialize the structure TPDFRastertImage and create a new thread in the priority lower or
Now you can start the thread. The thread executes only the function RenderPage(), finish!
The window must be updated via the TOnUpdateWindow callback function.
When the user requests to load another page or PDF file then call rasAbort(m_RasPtr); from
the main thread (m_RasPtr represents the instance pointer of the rasterizer), wait until the
thread returns and delete it.
Now you can load another file, page, or change the zoom settings and so on.
Make sure that you don't execute non-thread-safe code in the TOnUpdateWindow callback function
or in the error callback function.
Especially the error callback function should not directly add the error message to a list component
or something similar when the component is not thread-safe. Note that such components try to
render the text when it is added to the list. Such an action is critical when performed from another

Previous topic: UpdateOnImageCoverage limit, The return value, Multi-Threading strategies

Next topic: How to save the image on disk?