Hi am using a system.drawing.printing.printdocument control on a
VB Form.
I am calling my printDoc method from a background worker and it is printing the image fine.
However i want to
delete the image after printing so i have tried putting in an io.file.delete in my background workers "RunWorker_Completed Event" like this to try and detect when the file could be removed.
Code:
While 1
Try
IO.File.Delete(CStr(e.Result))
Exit While
Catch ex As Exception
System.Threading.Thread.Sleep(3000)
End Try
End While
ListBox1.Items.Add("Printed " & CStr(e.Result))
The file spools to the printer fine and then prints, i am unable to delete the file untill after i close my form , so the io.delete code just loops forever.
The PrintDocument1_EndPrint Event is being fired correctly, and i would
rather delete the printed job in the Background Workers DOWork Event as the way im trying now my GUI is being
locked up with the io.delete inside a While Loop.
Can anyone help with this?
I have posted all the code below :)
Regards
Entire code im using
Code:
Public Class Form1
Dim printJob01 As String = ""
Dim printer01 As String = "\\myServer\ittlaser"
Dim printJob01Done As Boolean = False
Sub _printDoc(ByVal printer As String)
'---------PRINTER
PrintDocument1.PrinterSettings.PrinterName = printer
'----------LANDSCAPE
PrintDocument1.DefaultPageSettings.Landscape = False
'----------MARGINS
Dim pm As New Printing.Margins(0, 0, 0, 0)
PrintDocument1.DefaultPageSettings.Margins = pm
PrintDocument1.OriginAtMargins = True
'----------PAPERSIZE
Dim ps As New Printing.PaperSize
ps.RawKind = Printing.PaperKind.A3
PrintDocument1.DefaultPageSettings.PaperSize = ps
'PrintDocument1.DefaultPageSettings.PaperSize = New Printing.PaperSize("Custom", 1169, 1654)
'---------RESOLUTION ------------DOES THIS WORK?
'Dim pr As New Printing.PrinterResolution
'pr.Kind = Printing.PrinterResolutionKind.High
'PrintDocument1.DefaultPageSettings.PrinterResolution = pr
'---------COLOUR
PrintDocument1.DefaultPageSettings.Color = False
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
printJob01Done = True
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim g As Graphics = e.Graphics
g.DrawImage(Image.FromFile(printJob01), 0, 0, 1155, 1654)
'g.DrawImage(Image.FromFile("C:\1\sing.png"), 0, 0)
End Sub
Private Sub printWorker01_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles printWorker01.DoWork
printJob01 = CStr(e.Argument)
_printDoc(printer01)
While 1
If printJob01Done = False Then
System.Threading.Thread.Sleep(3000)
Continue While
End If
Try
e.Result = CStr(e.Argument)
Exit While
Catch ex As Exception
System.Threading.Thread.Sleep(3000)
End Try
End While
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
printWorker01.RunWorkerAsync("c:\1\1.png")
End Sub
Private Sub printWorker01_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles printWorker01.RunWorkerCompleted
While 1
Try
IO.File.Delete(CStr(e.Result))
Exit While
Catch ex As Exception
System.Threading.Thread.Sleep(3000)
End Try
End While
ListBox1.Items.Add("Printed " & CStr(e.Result))
End Sub
End Class