VB enumPrinters hangs

153 Views Asked by At

In the VB app I've just inherited from a colleague (now gone somewhere else), I have to solve a problem where the app just hangs and doesn't execute anything.

I've come to the conclusion that the problems lays here:

For Each printer In System.Drawing.Printing.PrinterSettings.InstalledPrinters

which is called, in my source code, exactly like this:

    Protected Overridable Sub SetPrinter(ByRef data As IData, ByRef reportdoc As ReportDocument)
        Dim printer As String
        Dim paperorientation As String
        Dim papersource As String
        Dim papersize As String
        Dim devicename As String
        devicename = data.P_DeviceName
        If Not devicename Is Nothing AndAlso devicename.Trim <> "" Then
            paperorientation = reportdoc.PrintOptions.PaperOrientation
            papersize = reportdoc.PrintOptions.PaperSize
            papersource = reportdoc.PrintOptions.PaperSource
            devicename = devicename.Trim.ToUpper()
            For Each printer In System.Drawing.Printing.PrinterSettings.InstalledPrinters
                If printer.ToUpper() = devicename Then
                    reportdoc.PrintOptions.PrinterName = printer
                    reportdoc.PrintOptions.PaperOrientation = CShort(paperorientation)
                    reportdoc.PrintOptions.PaperSource = CShort(papersource)
                    If CDbl(papersize) > 0 And CDbl(papersize) < 42 Then
                        reportdoc.PrintOptions.PaperSize = CShort(papersize)
                    End If
                    Exit For
                End If
            Next
        End If
    End Sub

I guess that it is getting stuck when interrogating Windows for the printers list.

For some reason, I'm not getting any answer from Windows about the printers (all network printers). Please note that this same code works just fine for thousands of times before failing. The only way I've found to reset is to reboot (restarting the app or the spooler isn't enough to make everything ok)

I haven't yet had the time to study the technology nor the code. What can I try next?

0

There are 0 best solutions below