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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.