Resolving threading issues in code that uses PDFView class.

Q:

We have implemented a search function that iterates over pages,
extracts text runs (ElementReader), and annotates matches. The
application is also using PDFView class to render the PDF.

If a search match is found and another search is initiated in rapid
succession, the PDFNet api throws exceptions (non-deterministically).
The problem seems to related to threading issue, however our code that
uses PDFView is only single threaded.
----
A:

The problem is that even though your code is using only a single
thread, PDFView class is using another thread for interactive
rendering. This means that whenever you are accessing the document you
need to lock it. For example:

pdfdoc.Lock();
... read/write/edit the document...
pdfdoc.Unlock()

P.S. The upcoming version of PDFNet will have built-in support for
text search however users will still be able to disable this feature
and implement their custom methods.