Subject: [resolved] File is 'locked'? after printing.
Posted By: janama Post Date: 4/18/2008 9:39:04 PM
  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.


        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


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








Reply By: samjudson Reply Date: 4/19/2008 4:03:19 AM
See if this works:

g.DrawImage(Image.FromFile(printJob01), 0, 0, 1155, 1654)
g.Dispose()


/- Sam Judson : Wrox Technical Editor -/
Reply By: janama Reply Date: 4/19/2008 4:52:26 AM
Hi Sam, thanks for reply.

Still no good though im sorry to say.

In the re-vamped code below still getting the following exception in my "Try" statement to do the io.delete on the printed file. Not until i close my form am i able to manually delete the file via Windows Explorer, whilst the form is open Windows also refuses to delete the file even though the file has printed already.

"The process cannot access the file 'c:\1\1.png' because it is being used by another process."}    System.Exception

Code Im Using


    Dim printJob01 As String = ""
    Dim printer01 As String = "5000PS"
    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)
        g.Dispose()

    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)
                IO.File.Delete(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


Any ideas to help with this?

Regards



Reply By: samjudson Reply Date: 4/19/2008 5:52:35 AM
http://msdn2.microsoft.com/en-us/library/stf701f5.aspx

quote:
The file remains locked until the Image is disposed.


So, try this:


Dim i as Image = Image.FromFile(printJob01)
Dim g As Graphics = e.Graphics
g.DrawImage(i, 0, 0, 1155, 1654)

i.Dispose()
g.Dispose()


/- Sam Judson : Wrox Technical Editor -/
Reply By: janama Reply Date: 4/20/2008 1:03:42 AM
This is great Sam, works a treat , many thanks and my gratitude to you.

Regards



Go to topic 70683

Return to index page 1