I have seen a few similar questions, but I hope this is not quite a duplicate. I am using the FO.NET project found here: https://fonet.codeplex.com/ to generate PDF files from within my VB.NET application.
The Fonet.dll
and Fonet.exe
files are both included with the application, and I am calling it like so:
Public Sub FormatObjectToPdf(intRxNo As Integer, strSourceFileName As String)
Dim startInfo As New ProcessStartInfo
Dim strPdfFile As String = StrRootPath & "Paperwork\" & intRxNo & "M.pdf"
' if the PDF file already exists, no need to re-create it
If Not File.Exists(strPdfFile) Then
Try
startInfo.Arguments = "-fo """ & strSourceFileName & """ -pdf """ & strPdfFile & """"
startInfo.FileName = StrAppPath & "FO.NET\fonet.exe"
startInfo.UseShellExecute = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
Using proc As Process = Process.Start(startInfo)
proc.WaitForExit()
If proc.HasExited Then
proc.Dispose()
End If
End Using
Catch ex As Exception
Call InsertLog("ErrorLog", "FormatObjectToPdf: " & ex.Message, ex)
MessageBox.Show(ex.Message, "Create PDF", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
' wait 3 seconds to allow file to be released
Threading.Thread.Sleep(3000)
' delete the source FO file when processing is complete
If File.Exists(strSourceFileName) Then
Try
File.Delete(strSourceFileName)
Catch iEx As IOException
Call InsertLog("ErrorLog", "[IOException]: Could Not delete file '" & strSourceFileName & "': " & iEx.Message)
Catch ex As Exception
Call InsertLog("ErrorLog", "Error deleting file '" & strSourceFileName & "': " & ex.Message, ex)
End Try
End If
End Sub
My problem is that occasionally the process will fail, and an unhandled exception will popup and the Fonet.exe
application will crash (not the calling application). If I re-run the exact same call, it usually works. It is only happening occasionally, but it seems to be increasingly frequent. Is there a way to catch the exception from the external program for debugging? Or some other way to see what is causing it to crash, so that I can try to fix it?