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

codefingerAsked:
Who is Participating?
 
nffvrxqgrcfqvvcCommented:
I see a couple of problems with this approach. Process.Start() should launch there default image viewer/editor associated with the extension. This is Okay but the problem is that even if you use WaitForNext() it doesn't mean the process that was opened is the same process that would be used for editing the image. What happens in this case is the WaitForExit() would return before the user even started to begin any type of editing. I don't think your error is because the file is open but our error is from the fact that you attempt to delete the file while it's still opened and the scenerio mentioned above could be one of many reasons why this happens.
I would take a different approach and have a (Reload or Refresh) feature instead of automation type feature. If it's a personal project that you must (Automate) you might be able to hack around such an issue by creating MD5 hash or FileSystemWatcher but I don't think these are very good solutions.
0
 
codefingerAuthor Commented:
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.)
0
 
nffvrxqgrcfqvvcCommented:

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.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
CodeCruiserCommented:
>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.
0
 
nffvrxqgrcfqvvcCommented:
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.
0
 
codefingerAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.