嗨stackoverflow社区...
我有点难过。我编写了一个应用程序来控制收集打印统计信息,每个打印作业都由一个新线程管理,并且作为控制台应用程序,代码可以很好地工作。当我尝试在服务中初始化相同的线程队列工作者时,它不会通过它要启动的类运行。这只发生在 Windows 8 工作站上。
以下是一些代码片段。
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim tmp As New pmon_Audit
objwriter.AutoFlush = True
tmp.StartAudit()
End Sub
Imports SpoolMonitor
Imports System.Printing
Imports System.Threading
Imports Amib.Threading
Imports System.Net.Sockets
Public Class pmon_Audit
Friend WithEvents pmon As New PrinterMonitorComponent
Public ThreadPool As New SmartThreadPool(10, 1000, 0)
Public Sub StartAudit()
pmon.MonitorJobAddedEvent = True
'' Add printer to monitor
Try
For Each p As PrinterInformation In New PrinterInformationCollection
Try
pmon.AddPrinter(p.PrinterName)
WriteLog(String.Format("Printer {0} added to monitor", p.PrinterName))
Catch ex As Exception
WriteLog(ex.ToString)
End Try
Next p
Catch ex As Exception
WriteLog(ex.ToString)
End Try
End Sub
''' <summary>
''' Event that gets fired each time a job is added
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub pmon_jobAdded(ByVal sender As Object, ByVal e As PrintJobEventArgs) Handles pmon.JobAdded
Dim myPrintServer As New LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer)
Try
Dim pq As PrintQueue = myPrintServer.GetPrintQueue(e.PrintJob.PrinterName)
Dim jobAdded As New pmon_JobAdded(e.PrintJob)
pq.GetJob(e.PrintJob.JobId).Pause()
WriteLog("JobID " + e.PrintJob.JobId.ToString + " is paused")
ThreadPool.QueueWorkItem(New Amib.Threading.Action(AddressOf jobAdded.run))
Catch ex As Exception
WriteLog(ex.ToString)
End Try
Public Function run()
Dim myPrintServer As New LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer)
Dim pq As PrintQueue = myPrintServer.GetPrintQueue(pjReceived.PrinterName)
curPT = pq.CurrentJobSettings.CurrentPrintTicket
If jb Is Nothing Then
Throw New ArgumentNullException("printJobRecord", "printJobRecord is null.")
End If
Try
WriteLog("JobID " + pjReceived.JobId.ToString + " added to monitor")
jb.JobID = pjReceived.JobId
While pjReceived.Spooling
Thread.Sleep(800)
pjReceived.Update()
End While
Try
If pjReceived.Color = True Then
jb.Colour = 2
Else
jb.Colour = 0
End If
WriteLog("JobID " + pjReceived.JobId.ToString + " Colour = " + jb.Colour.ToString)
Catch ex As Exception
jb.Colour = 1
End Try
Try
jb.Account = pjReceived.UserName
WriteLog("JobID " + pjReceived.JobId.ToString + " Account = " + jb.Account)
Catch ex As Exception
jb.Account = "Admin"
End Try
Try
jb.Application = pjReceived.Document
WriteLog("JobID " + pjReceived.JobId.ToString + " Application = " + jb.Application)
Catch ex As Exception
WriteLog(ex.ToString)
End Try
Try
If pq.CurrentJobSettings.CurrentPrintTicket.CopyCount > pjReceived.Copies Then
jb.Copies = pq.CurrentJobSettings.CurrentPrintTicket.CopyCount
ElseIf pq.UserPrintTicket.CopyCount > pjReceived.Copies Then
jb.Copies = pq.UserPrintTicket.CopyCount
Else
jb.Copies = pjReceived.Copies
End If
WriteLog("JobID " + pjReceived.JobId.ToString + " Copies = " + jb.Copies.ToString)
Catch ex As Exception
jb.Copies = 1
End Try
Try
jb.DeviceName = pjReceived.PrinterName
WriteLog("JobID " + pjReceived.JobId.ToString + " PrinterName = " + jb.DeviceName)
Catch ex As Exception
End Try
Try
jb.DataType = pjReceived.DataType
Catch ex As Exception
End Try
Try
jb.Document = pjReceived.Document
WriteLog("JobID " + pjReceived.JobId.ToString + " Document = " + jb.Document)
Catch ex As Exception
jb.Document = "UNKNOWN"
End Try
Try
jb.Driver = pjReceived.DriverName
WriteLog("JobID " + pjReceived.JobId.ToString + " Driver = " + jb.Driver)
Catch ex As Exception
jb.Driver = "UNKOWN"
End Try
Try
If pq.CurrentJobSettings.CurrentPrintTicket.Duplexing > pq.UserPrintTicket.Duplexing Then
jb.Duplex = pq.CurrentJobSettings.CurrentPrintTicket.Duplexing
Else
jb.Duplex = pq.UserPrintTicket.Duplexing
End If
If jb.Duplex = "twoSided" Then
jb.Duplex = "1"
Else
jb.Duplex = "0"
End If
WriteLog("JobID " + pjReceived.JobId.ToString + " Duplex = " + jb.Duplex)
Catch ex As Exception
jb.Duplex = "0"
End Try
Try
jb.JobDate = pjReceived.Submitted
WriteLog("JobID " + pjReceived.JobId.ToString + " JobDate = " + jb.JobDate)
Catch ex As Exception
jb.JobDate = Date.Now
End Try
Try
jb.JobSize = pjReceived.JobSize
Catch ex As Exception
jb.JobSize = "0"
End Try
Try
jb.Location = Dns.GetHostName.ToUpper
WriteLog("JobID " + pjReceived.JobId.ToString + " Location = " + jb.Location)
Catch ex As Exception
jb.Location = "UNKOWN"
End Try
Try
jb.Media = pq.CurrentJobSettings.CurrentPrintTicket.PageMediaSize.PageMediaSizeName
jb.Media = pjReceived.PaperKind
WriteLog("JobID " + pjReceived.JobId.ToString + " Media = " + jb.Media)
Catch ex As Exception
jb.Media = "201"
End Try
Try
jb.NUP = pq.CurrentJobSettings.CurrentPrintTicket.PagesPerSheet
Catch ex As Exception
jb.NUP = 0
End Try
Try
jb.PageCount = pjReceived.TotalPages
WriteLog("JobID " + pjReceived.JobId.ToString + " PageCount = " + jb.PageCount.ToString)
Catch ex As Exception
jb.PageCount = 1
End Try
Try
jb.PortName = pq.QueuePort.Name
Catch ex As Exception
End Try
Try
myPrintServer.Dispose()
Catch ex As Exception
WriteLog(ex.ToString)
End Try
Catch ex As Exception
pq.GetJob(pjReceived.JobId).Resume()
myPrintServer.Dispose()
WriteLog(ex.ToString)
End Try
GC.Collect()
Return 0
End Function