Vb.net 线程池未在服务中执行

Vb.net 线程池未在服务中执行,vb.net,multithreading,Vb.net,Multithreading,嗨,斯塔克弗社区 我有点不知所措。我已经编写了一个应用程序来控制收集打印统计数据,每个打印作业都由一个新线程管理,作为一个控制台应用程序,代码起到了处理作用。当我尝试初始化服务中的同一线程队列工作程序时,它不会运行在它要启动的类中。这仅在Windows 8 works工作站上发生 下面是一些代码片段 Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your s

嗨,斯塔克弗社区

我有点不知所措。我已经编写了一个应用程序来控制收集打印统计数据,每个打印作业都由一个新线程管理,作为一个控制台应用程序,代码起到了处理作用。当我尝试初始化服务中的同一线程队列工作程序时,它不会运行在它要启动的类中。这仅在Windows 8 works工作站上发生

下面是一些代码片段

    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
protectedoverrides子启动(ByVal args()作为字符串)
'在此处添加代码以启动您的服务。这种方法应该能解决问题
“在运动中,以便您的服务能够完成其工作。
Dim tmp作为新的pmon_审计
objwriter.AutoFlush=True
tmp.StartAudit()
端接头
导入假脱机监视器
导入系统。打印
导入系统线程
导入Amib.Threading
导入System.Net.Sockets
公共类pmon_审计
Friend with events pmon作为新的PrinterMonitor组件
公共线程池作为新的SmartThreadPool(10,1000,0)
公共子StartAudit()
pmon.MonitorJobAddedEvent=True
“”将打印机添加到监视器
尝试
对于新PrinterInformation集合中的每个p作为PrinterInformation
尝试
pmon.AddPrinter(p.PrinterName)
WriteLog(String.Format(“将打印机{0}添加到监视器”,p.PrinterName))
特例
WriteLog(例如ToString)
结束尝试
下一个p
特例
WriteLog(例如ToString)
结束尝试
端接头
''' 
每次添加作业时被激发的“”事件
''' 
''' 
''' 
私有子pmon_jobAdded(ByVal sender作为对象,ByVal e作为PrintJobEventArgs)处理pmon.jobAdded
将myPrintServer设置为新的LocalPrintServer(PrintSystemDesiredAccess.AdministrationServer)
尝试
Dim pq As PrintQueue=myPrintServer.GetPrintQueue(e.PrintJob.PrinterName)
添加的Dim作业作为新的pmon_作业添加(例如打印作业)
pq.GetJob(e.PrintJob.JobId).Pause()
WriteLog(“JobID”+e.PrintJob.JobID.ToString+“暂停”)
ThreadPool.QueueWorkItem(新Amib.Threading.Action(AddressOf jobAdded.run))
特例
WriteLog(例如ToString)
结束尝试
公共功能运行()
将myPrintServer设置为新的LocalPrintServer(PrintSystemDesiredAccess.AdministrationServer)
Dim pq As PrintQueue=myPrintServer.GetPrintQueue(pjReceived.PrinterName)
curPT=pq.CurrentJobSettings.CurrentPrintTicket
如果jb什么都不是
抛出新ArgumentNullException(“printJobRecord”,“printJobRecord为null”)
如果结束
尝试
WriteLog(“JobID”+pjReceived.JobID.ToString+“添加到监视器”)
jb.JobID=pjReceived.JobID
当PJR接收时。后台处理
线程。睡眠(800)
pjReceived.Update()
结束时
尝试
如果pjReceived.Color=True,则
jb.颜色=2
其他的
jb.颜色=0
如果结束
WriteLog(“JobID”+pjReceived.JobID.ToString+”color=“+jb.color.ToString”)
特例
jb.颜色=1
结束尝试
尝试
jb.Account=pjReceived.UserName
WriteLog(“JobID”+pjReceived.JobID.ToString+”Account=“+jb.Account”)
特例
jb.Account=“Admin”
结束尝试
尝试
jb.Application=pjReceived.Document
WriteLog(“JobID”+pjReceived.JobID.ToString+”应用程序=“+jb.Application”)
特例
WriteLog(例如ToString)
结束尝试
尝试
如果pq.CurrentJobSettings.CurrentPrintTicket.CopyCount>pjReceived.Copies,则
jb.Copies=pq.CurrentJobSettings.CurrentPrintTicket.CopyCount
ElseIf pq.UserPrintTicket.CopyCount>pjReceived。然后复制
jb.Copies=pq.UserPrintTicket.CopyCount
其他的
jb.Copies=pjReceived.Copies
如果结束
WriteLog(“JobID”+pjReceived.JobID.ToString+”Copies=“+jb.Copies.ToString”)
特例
jb.Copies=1
结束尝试
尝试
jb.DeviceName=pjReceived.PrinterName
WriteLog(“JobID”+pjReceived.JobID.ToString+”PrinterName=“+jb.DeviceName”)
特例
结束尝试
尝试
jb.DataType=pjReceived.DataType
特例
结束尝试
尝试
jb.Document=pjReceived.Document
WriteLog(“JobID”+pjReceived.JobID.ToString+”Document=“+jb.Document”)
特例
jb.Document=“未知”
结束尝试
尝试
jb.Driver=pjReceived.DriverName
WriteLog(“JobID”+pjReceived.JobID.ToString+”Driver=“+jb.Driver”)
特例
jb.Driver=“未知”
结束尝试
尝试
如果pq.CurrentJobSettings.CurrentPrintTicket.Duplexing>pq.UserPrintTicket.Duplexing,则
jb.Duplex=pq.CurrentJobSettings.CurrentPrintTicket.Duplexing
其他的
jb.Duplexing=pq.UserPrintTicket.Duplexing
如果结束
如果jb.Duplex=“twoSided”,则
jb.Duplex=“1”
其他的
jb.Duplex=“0”
如果结束
WriteLog(“JobID”+pjReceived.JobID.ToString+”Duplex=“+jb.Duplex)
特例
jb.Duplex=“0”
结束尝试
尝试
J