Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 18th, 2008, 09:39 PM
Registered User
 
Join Date: Apr 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default [resolved] File is 'locked'? after printing.

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
 
Old April 19th, 2008, 04:03 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

See if this works:

Code:
g.DrawImage(Image.FromFile(printJob01), 0, 0, 1155, 1654)
g.Dispose()
/- Sam Judson : Wrox Technical Editor -/
 
Old April 19th, 2008, 04:52 AM
Registered User
 
Join Date: Apr 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Code:
    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



 
Old April 19th, 2008, 05:52 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

http://msdn2.microsoft.com/en-us/library/stf701f5.aspx

Quote:
quote:The file remains locked until the Image is disposed.
So, try this:

Code:
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 -/
 
Old April 20th, 2008, 01:03 AM
Registered User
 
Join Date: Apr 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is great Sam, works a treat , many thanks and my gratitude to you.

Regards







Similar Threads
Thread Thread Starter Forum Replies Last Post
[Resolved] Error: File used by another process snufse .NET Framework 2.0 2 February 18th, 2008 09:20 AM
Access 2000 database file locked up jeffcflam Access VBA 1 October 19th, 2006 10:38 AM
Printing to file Sonreir VB How-To 0 August 2nd, 2004 09:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.