Results 1 to 5 of 5

Thread: Printing to PDF problems

  1. #1
    Member
    Join Date
    2010-06
    Location
    Guildford, UK
    Posts
    13
    Login to Give a bone
    0

    Default Printing to PDF problems

    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?

  2. #2
    Active Member
    Join Date
    2011-11
    Location
    Saint-Omer, Pas-de-Calais, France
    Posts
    58
    Login to Give a bone
    0

    Default Re: Printing to PDF problems

    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).

  3. #3
    Member
    Join Date
    2010-06
    Location
    Guildford, UK
    Posts
    13
    Login to Give a bone
    0

    Default Re: Printing to PDF problems

    Thanks for the reply.

    I have used DoEvents in previous code. I started looking around and found this http://stackoverflow.com/questions/6...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.

  4. #4
    Active Member
    Join Date
    2011-11
    Location
    Saint-Omer, Pas-de-Calais, France
    Posts
    58
    Login to Give a bone
    0

    Default Re: Printing to PDF problems

    In your case, you should use a FileSystemWatcher to wait for the creation of the file instead of a loop.

  5. #5
    Member
    Join Date
    2010-06
    Location
    Guildford, UK
    Posts
    13
    Login to Give a bone
    0

    Default Re: Printing to PDF problems

    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

Similar Threads

  1. Printing Problems
    By rob.127056 in forum Revit Architecture - General
    Replies: 7
    Last Post: 2012-08-03, 02:49 PM
  2. printing problems with JPG
    By davel.254027 in forum Revit MEP - General
    Replies: 6
    Last Post: 2010-07-21, 01:39 PM
  3. Problems printing since SP3
    By nubrun.188780 in forum Revit MEP - General
    Replies: 1
    Last Post: 2008-12-11, 04:52 PM
  4. Printing Problems - need help!
    By deepikapadam in forum Revit Architecture - General
    Replies: 5
    Last Post: 2005-12-02, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •