CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Printing from a windows service. Problem.

    I have been working on a project for a customer which prints some labels from collected data. The final step was to build a service which would print the tickets automatically as needed. I had tested everything in a regular exe before moving the code to the service and all was well but when I created the service nothing is printing. I added some enteries into the log to try and track down the issue and I could see that the service was running, it enters the routine that gets the data to be printed and attempts to print it but the print event does not appear to fire and then the code seems to jump out of the routine that called the .Print method. I am totally confused at this point.
    Here is a segment of the routine that formats the data for printing. I have highlighted the lines I added for debugging purposes and of course there is a lot more code in this main procedure but does not seem important for this issue.
    Code:
    If Dr.HasRows Then
                Me.EventLog.WriteEntry("Got records")
                Dim cmd2 As New SqlCommand
                cmd2.Connection = New SqlConnection(ConnectionString)
                cmd2.Connection.Open()
                While Dr.Read
                    tmpStockroom = Dr(1)
                    If LastTicket = String.Empty Then
                        StockRoom = Dr(1)
                        LastTicket = Dr(0).ToString
                    Else
                        If Not (LastTicket = Dr(0).ToString) Or StockRoom <> Dr(1) Then
                            TicketData = Ticket.ToString
                            x = Ticket.Length
                            If StockRoom = 1 Then
                                Me.EventLog.WriteEntry("1: Attempting to print to " & Printer1)
                                PrintDocument1.Print()
                            Else
                                Me.EventLog.WriteEntry("2: Attempting to print to " & Printer2)
                                PrintDocument2.Print()
                            End If
    ' ....... snip .......
     End If
            Me.EventLog.WriteEntry("Exit Print Event")
            Dr.Close()
    I have 2 printdocuments included
    Code:
     Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Me.EventLog.WriteEntry("Top of print event 1")
            Dim fnt As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
            e.Graphics.DrawString(TicketData, fnt, Brushes.Black, 0, 0)
            e.HasMorePages = False
            Me.EventLog.WriteEntry("Bottom of print event 1")
        End Sub
    Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
            Me.EventLog.WriteEntry("Top of print event 2")
            Dim fnt As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
            e.Graphics.DrawString(TicketData, fnt, Brushes.Black, 0, 0)
            e.HasMorePages = False
            Me.EventLog.WriteEntry("Bottom of print event 2")
        End Sub
    When I look at the event log I see something like this
    X24400 Service stopped successfully.
    X24400 AMG Printing Service stopped
    X24400 1: Attempting to print to \\p42800\HP LaserJet 1020
    X24400 Got records
    X24400 1: Attempting to print to \\p42800\HP LaserJet 1020
    X24400 Got records
    X24400 1: Attempting to print to \\p42800\HP LaserJet 1020
    X24400 Got records
    X24400 1: Attempting to print to \\p42800\HP LaserJet 1020
    X24400 Got records
    X24400 1: Attempting to print to \\p42800\HP LaserJet 1020
    X24400 Got records
    X24400 1: Attempting to print to \\p42800\HP LaserJet 1020
    X24400 Got records
    X24400 Service started successfully.
    X24400 AMG Printing Service started
    There is no sign that the print event was entered in the log as well as the entry I have at the bottom of the sub not being there as if when it hits the print statement it just jumps out of the routine then enters it again a few seconds later as there are still tickets that need to be printed.
    Also if the code was working properly there is data there to print to both printers it should do 1 print to printer 1 and then should do one to printer 2 but only the first one ever shows up and nothing prints.
    The service is set to run in the Local System account if that makes a difference.
    Anyone have any idea what could be the problem here?

    Edit: After more research it appears that it has something to do with the account not having access to the printer. I have tried using my own account and the system administrators account but both with the same results.

    I have decided to install an normal windows exe for now to do that task but I really would like to do this with a service so that no user needs to be logged in on the machine and be able to run it on the server.

    Any ideas how I can get this to work?
    Last edited by DataMiser; October 26th, 2012 at 09:40 AM.
    Always use [code][/code] tags when posting code.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured