我可以通过限制方法允许执行的时间来避免可能的挂起吗?

发布于 2024-11-18 22:27:44 字数 361 浏览 6 评论 0原文

我正在使用外部 DLL (pdfsharp) 打开(然后操作)大量 PDF 文件。我使用:

Dim inputDocument = Pdf.IO.PdfReader.Open(PDFPath, IO.PdfDocumentOpenMode.ReadOnly)

问题是 - 它似乎挂在某些罕见的文件上。我似乎没有任何超时 - 它只是在这条线上挂了几个小时。我们用这个代码读取了数千个文件,总是在小文件上,所以我想一个快速的解决方法可能是在这个方法花费超过一两秒的时间时以某种方式超时。但我没有看到一个简单的方法来做到这一点。我希望避免旋转工作线程。

关于如何限制该线程允许的执行时间的任何想法,或者是否有更好(但简单)的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡 2024-11-25 22:27:44

Open() 调用不应挂起。绝不。如果您向我们提供了导致 Open() 挂起的文件,我们可以对此进行调查。

你的程序在服务器上运行吗?您使用 PDFsharp 的 DEBUG 版本吗?也许只是一个简单的 Debug.Assert() 被触发,但没有人能回答它。使用 RELEASE 构建可以解决这个问题。

The Open() call should not hang. Never. If you provide us with a file that causes Open() to hang, we can investigate this.

Does your program run on a server? Do you use a DEBUG build of PDFsharp? Maybe it's just a simple Debug.Assert() that is triggered, but noone can answer it. Using a RELEASE build would solve this.

狂之美人 2024-11-25 22:27:44

我们最终通过创建 AbortableBackgroundWorker 解决了这个问题。我不确定这最终是谁的代码 - 但我们在网上找到了它并在这里分享。在极少数情况下,其中一个 PDF 挂起 PdfSharp Open() 调用,我们会中止后台工作程序。

  Public Class AbortableBackgroundWorker
    Inherits BackgroundWorker

    Private workerThread As Thread

    Protected Overrides Sub OnDoWork(e As DoWorkEventArgs)
        workerThread = Thread.CurrentThread
        Try
            MyBase.OnDoWork(e)
        Catch generatedExceptionName As ThreadAbortException
            e.Cancel = True
            'We must set Cancel property to true!
            'Prevents ThreadAbortException propagation
            Thread.ResetAbort()
        End Try
    End Sub


    Public Sub Abort()
        If workerThread IsNot Nothing Then
            workerThread.Abort()
            workerThread = Nothing
        End If
    End Sub

End Class

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.

  Public Class AbortableBackgroundWorker
    Inherits BackgroundWorker

    Private workerThread As Thread

    Protected Overrides Sub OnDoWork(e As DoWorkEventArgs)
        workerThread = Thread.CurrentThread
        Try
            MyBase.OnDoWork(e)
        Catch generatedExceptionName As ThreadAbortException
            e.Cancel = True
            'We must set Cancel property to true!
            'Prevents ThreadAbortException propagation
            Thread.ResetAbort()
        End Try
    End Sub


    Public Sub Abort()
        If workerThread IsNot Nothing Then
            workerThread.Abort()
            workerThread = Nothing
        End If
    End Sub

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文