View Full Version : 2014 Printing to PDF problems
Paul_Collins
2014-09-15, 04:14 PM
I have an Add-in which prints to PDF and sets the filename according to a set of rules chosen by the user.
This has been rolled out across the company to about 50 workstations. We use Adobe Acrobat XI Pro as our PDF printer.
The problem I am having is that sometimes the Add-in will hang but it is unpredictable as to when this will happen, making it hard to find a solution.
A solution when this happens is to use task manager to kill the acrotray.exe process which then lets the Add-in continue successfully.
I have tried several ways of printing from the API, without improvement. I have a loop after the submit print code to allow the first print to be sent before moving on to the next.
I open the views first then select them one at a time to make that the active view then submit the active view to the print manager.
My code at the moment looks like this:
'print the active view
MyPrintMgr.SubmitPrint()
'wait until file is created before proceeding
Dim count As Integer = 0
Do Until File.Exists(FileName)
If count = 60 Then 'change this value to adjust timeout
Exit Do 'prevent being stuck in loop for ever
End If
Threading.Thread.Sleep(1000) 'wait 1 second
count = count + 1
Loop
End If
Any ideas?
Maxence DELANNOY
2014-09-16, 07:38 AM
You should add an System.Windows.Forms.Application.DoEvents() in your loop to let Windows process the messages. Use a smaller duration for your loop (100 ms should be good. 1 s is an eternity for a computer) and increase the number of loops to 600.
For the PDF generation, it seems that it is acrotray.exe which is hanging and there is nothing you can do for that (you can only kill the process when a timeout is reached and report the problem to the user).
Paul_Collins
2014-09-16, 09:13 AM
Thanks for the reply.
I have used DoEvents in previous code. I started looking around and found this http://stackoverflow.com/questions/6728267/vb-net-application-doevents-function-halt-and-crash-application-in-windows-vis which is a discussion of the pros and cons of using DoEvents and also suggesting the use of BackgroundWorker as a better alternative. I am going to modify my code and see if it helps.
Maxence DELANNOY
2014-09-17, 07:29 AM
In your case, you should use a FileSystemWatcher to wait for the creation of the file instead of a loop.
Paul_Collins
2014-10-16, 02:14 PM
Thanks for your suggestions which have helped me to fix it.
Further investigation showed that it was the MyPrintMgr.SubmitPrint() line that was occasionally causing Acrobat to hang.
By starting a multi-thread timer before issuing the print command, then if the printing is ok the timer is stopped. If the printing hangs or the file takes too long to create (using filesystemwatcher) then the acrotray.exe process is stopped.
'set a timer running to catch acrotray freezing
'if SubmitPrint not finished after timer interval
If var_PDF_UseTimer Then Timers.StartTimer()
'print the active view
MyPrintMgr.SubmitPrint()
'stop the timer from firing
If var_PDF_UseTimer Then Timers.CancelTimer()
If var_PDF_UseTimer Then WaitForFile(FolderName, FileName)
Public Sub StartTimer()
' Starts a new thread.
Dim th As New System.Threading.Thread(AddressOf CountDown)
CancelThread.Reset()
ThreadisCanceled.Reset()
th.Start()
'MsgBox("Started")
End Sub
Public Sub CancelTimer()
' Stops any threads started by the StartTask procedure.
' Notice that this thread both receives and sends
' synchronization events to coordiante the threads.
CancelThread.Set() ' Set CancelThread to ask the thread to stop.
End Sub
Private Sub CountDown()
Dim LoopCount As Integer
Dim Timer As Integer = var_PDF_Timeout
If var_PDF_Extended Then Timer = var_PDF_Timeout * 5
Dim Loops As Integer = Timer / 1000
' Run code in a While loop until set delay passes, or
' until CancelThread is set.
While Not CancelThread.WaitOne(0, False) And LoopCount < Loops
System.Threading.Thread.Sleep(1000) ' Sleep for one second.
LoopCount += 1
End While
If CancelThread.WaitOne(0, False) Then
'Acknowledge that the ManualResetEvent CancelThread is set.
ThreadisCanceled.Set()
'MsgBox("Canceling thread")
Else
'MsgBox("Thread is done")
KillProcess()
End If
End Sub
Public Sub KillProcess()
'kill the acrotray.exe process
Dim processList() As Process = Process.GetProcessesByName("acrotray")
'there may be more than one process...
For Each p As Process In processList
If InStr(p.ProcessName, "acrotray") > 0 Then
Try
p.Kill()
myTimerCount = myTimerCount + 1
'MsgBox("Acrotray interrupted")
Catch ex As Exception
End Try
End If
Next
End Sub
Public Sub WaitForFile(ByVal Foldername As String, ByVal Filename As String)
Try
'wait until file is created before proceeding
If Not System.IO.Directory.Exists(Foldername) Then Exit Sub
Dim Timer As Integer = var_PDF_Timeout
If var_PDF_Extended Then Timer = var_PDF_Timeout * 5
'Use a FileSystemWatcher to monitor the path and file for changes.
Dim fsw As New FileSystemWatcher(Foldername, System.IO.Path.GetFileName(Filename))
'must enable this for events to be fired
fsw.EnableRaisingEvents = True
If System.IO.File.Exists(Filename) Then Exit Sub
'Wait until the file has been created, or timeout
Dim result As IO.WaitForChangedResult = fsw.WaitForChanged(WatcherChangeTypes.All, Timer) ' 1000 = 1 second
If result.TimedOut Then
KillProcess()
End If
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.