【问题标题】:Clearing a Printer Queue correctly正确清除打印机队列
【发布时间】:2018-10-02 15:02:25
【问题描述】:

场景 - 创建一个能够在用户机器上添加和删除打印机的应用程序后,我遇到了一个关键错误:

1) 如果打印队列(System32\spool\PRINTERS)中存在打印作业,则不会删除打印机,这些是 SPL 和 SHD 文件

2) 如果有一个大小为 23,000kb 的文件,我需要一种方法来停止进程,直到命令完成

我当前的方法流程

                    Dim p As Process = New Process()
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                    p.StartInfo.FileName = "cmd.exe"

                    For i = 0 To 2
                        Select Case i
                            Case 0
                                'Stop spooler and dependencies
                                p.StartInfo.Arguments = "/c net stop spooler /yes"
                                p.Start()

                            Case 1
                                'Delete all queues within folder
                                p.StartInfo.Arguments = "/c del C:\Windows\System32\spool\PRINTERS\*.* /F /Q"
                                p.Start()

                                'Delete printer
                                printer.Delete()

                            Case 2
                                'Restart the spooler service
                                p.StartInfo.Arguments = "/c net start spooler"
                                p.Start()

                        End Select
                    Next                 

带线程

    Dim p As Process = New Process()
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                    p.StartInfo.FileName = "cmd.exe"

                    For i = 0 To 2
                        Select Case i
                            Case 0
                                'Stop spooler and dependencies
                                p.StartInfo.Arguments = "/c net stop spooler /yes"
                                p.Start()
                                'Allow time for the application to purge larger file sizes
                                Threading.Thread.Sleep(2500)
                            Case 1
                                'Delete all queues within folder
                                p.StartInfo.Arguments = "/c del C:\Windows\System32\spool\PRINTERS\*.* /F /Q"
                                p.Start()

                                'Delete printer
                                printer.Delete()

                            Case 2
                                'Restart the spooler service
                                p.StartInfo.Arguments = "/c net start spooler"
                                p.Start()

                                Threading.Thread.Sleep(2500)
                        End Select
                    Next

这两种方法都不能正常工作,应用程序没有足够的时间让后台处理程序服务停止。这意味着文件不会被删除,导致删除打印机时出错。

我希望从这个帖子中获得什么

我需要一些有关如何有效操作此程序的指导。非常感谢

【问题讨论】:

    标签: .net vb.net printing


    【解决方案1】:

    您可以在您的代码中停止该服务:

    Option Infer On
    Option Strict On
    
    Imports System.ServiceProcess
    '........
    
    Sub StopService(ByVal serviceName As String)
    
        Using sc = New ServiceController(serviceName)
            Dim ts = TimeSpan.FromSeconds(30)
            Try
                sc.Stop()
                sc.WaitForStatus(ServiceControllerStatus.Stopped, ts)
                log.AppendFormatLine("Stopped {0} service.", serviceName)
            Catch ex As InvalidOperationException
                log.AppendFormatLine("** Could not stop {0} service because {1}.", serviceName, ex.Message)
            Catch ex As TimeoutException
                log.AppendFormatLine("** Timeout after waiting {0} seconds for {1} service to stop.", ts.Seconds.ToString)
            End Try
        End Using
    
    End Sub
    

    【讨论】:

    • 这非常有效,但是,它揭示了我的应用程序中的一个新错误sigh。感谢安德鲁的帮助,荣誉
    猜你喜欢
    • 2014-05-14
    • 2018-02-18
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多