【发布时间】: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
这两种方法都不能正常工作,应用程序没有足够的时间让后台处理程序服务停止。这意味着文件不会被删除,导致删除打印机时出错。
我希望从这个帖子中获得什么:
我需要一些有关如何有效操作此程序的指导。非常感谢
【问题讨论】: