I am using an external DLL (pdfsharp) to open (then manipulate) lots of PDF files. I use:
Dim inputDocument = Pdf.IO.PdfReader.Open(PDFPath, IO.PdfDocumentOpenMode.ReadOnly)
Problem is – it seems to hang on certain, rare files. I don’t seem to get any timeout – it just hangs for hours on this line. We read thousands of files with this code, always on tiny files, so I was thinking that a quick workaround might be to somehow timeout if the this method takes more than a second or two. But I don’t see a simple way to do this. I am hoping to avoid spinning up a worker thread.
Any thoughts on how I might limit this threads allowed execution time, or is there a better (but simple) way?
We ended up working around this problem by creating an AbortableBackgroundWorker. I am not sure whose code this ended up being – but we found it online and sharing it here. In the rare case where one of the PDF’s hangs the PdfSharp Open() call, we abort the background worker.