Link to home
Start Free TrialLog in
Avatar of codefinger
codefingerFlag for United States of America

asked on

how to release file resources for further processing

My program displays an image in a picture box. The purpose of the attached code is to let the user step out of my application, edit the image with Paint Shop Pro or WinPaint or whatever, then when they return to my app the updates they have made are displayed in my application,  This almost works, however, I get the error

"The process cannot access the file  because it is being used by another process."

How can I release the file from use when they exit the other application?  (I have already tried disabling the indexing service, to be sure my problem was not coming from there.)

Thanks in advance.




Public Function EditDisplayedImage(ByRef lex As LastException) As Boolean

        
        Dim fsavedfilename As String
        Dim formname As String = Nothing
        Dim fc As HPFFAFormClass = Nothing
        Dim fp As PageFileClass = Nothing

        Dim pageno As Integer = 0
        Dim retval As Boolean = True
        Dim process_edit As Process = Nothing



        Try
            fc = bs.Current
            formname = fc.FormName
            fp = frm.ListBoxPageFiles.SelectedItem
            pageno = fp.Pageno
            fsavedfilename = System.Configuration.ConfigurationManager.AppSettings("UserFormPageDirectory") _
                             & formname & "-" & pageno.ToString & ".gif"

            If System.IO.File.Exists(fsavedfilename) Then
                System.IO.File.Delete(fsavedfilename)
            End If

            frm.PictureBox1.Image.Save(fsavedfilename)
            process_edit = Process.Start(fsavedfilename)
            process_edit.WaitForExit()
            Application.DoEvents()
            If fp.ConvertFileToImage(fsavedfilename, lex) Then
                frm.PictureBox1.Image = fp.MyImage
                Application.DoEvents()
                System.IO.File.Delete(fsavedfilename)
            Else
                retval = False
            End If


        Catch ex As Exception

            retval = False
            lex.Message = ex.Message
            If Not ex.InnerException Is Nothing Then
                lex.Detail = ex.InnerException.Message
            End If
            lex.Source = "FormManagerCtrl - EditDisplayedImage"
            lex.Code = "System Exception"

        End Try

        Return retval


    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of codefinger

ASKER

I am pretty sure you are completely wrong about WaitForExit() and I proved it with the debugger.  
Nothing else happens in my code until the user exits the application that was launched with Process.Start
(Which will be the image file editor associated with the file extension.)
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc


Scenerio:
VB program opens associated process.
The associated process is a (Viewer.exe) (The viewer doesn't let you directly edit the image)
The Viewer has a button called (Edit) (This is how you edit the image)
When you click this button the Viewer is closed and the (Editor) process is opened instead.
This would trigger WaitForExit() event. This depends on which program is being used while it might work for some users it won't for others.
Avatar of Nasir Razzaq
>The associated process is a (Viewer.exe) (The viewer doesn't let you directly edit the image)
>The Viewer has a button called (Edit) (This is how you edit the image)

Never came across such a program.

@codefinger
Do you get the error when the code after WaitForExit resumes? Try adding some Thread.Sleep to allow the other program shut completely and release its resources before you try to access the file.
Hi Code
It doesn't matter if you have never seen such a program. It's basic putting yourself in the users shoes 101. The OP only references (Users) and lists a couple of applications as if they aren't aware of what program will be used. You can't predict how a users image editing software will work and this is the reason why the logic fails. If this targeted a specific application that you could predict then the approach would be more appropriate. (OP hasn't confirmed a specific application so it's assumed it could be any application) When your application doesn't have control over task it's better to let the user decide what should be done next. This is why a more elegate solution is to just have a Refresh/Update/Wizard feature the user could issue when they have finished.
I was able to work around the problem by adding the time to the filename, so each time he external process is launched it is editing a different file.  Then I wait till the user exits the form to delete the edited files from the work directory.   Your comments were valuable in helping me arrive at this solution.  I have a pretty good idea which editor the user will be using (either Paint Shop Pro or Paint), but I will know what to watch for it a user finds another image editor that doesn't play nice.  Thanks.