|
-
Oct 15th, 2024, 08:15 AM
#1
Thread Starter
Lively Member
[RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
I'm trying to delete a png after displaying it in a ListView.
But when I try, I get the following error: "The process can not access the file b/c it is being used by another process."
The only "process" is my own app which just loaded the image. Obviously, I must close or terminate the process so the file can be deleted. Problem is, I didn't create a specific process or Handler to load the image in the first place:
Code:
For intCnt = 1 To intTotalNumberOfFiles
img = Image.FromFile(strSortedImges(intCnt))
ImageList2.Images.Add(img)
lsvCollection.SmallImageList = ImageList2 ' Reassign to list with icons.
lsvCollection.Items.Add(ParseItemName(strUnsortedNames(intCnt)), intCnt.ToString)
img = Nothing
' img.Dispose() doesn't work. Tried adding to fix "File in use" error when deleting file. Causes runtime error.
Next
...
FileIO.FileSystem.CopyFile(strSrcPath, strDestPath)
...
FileIO.FileSystem.DeleteFile(strSrcPath & "file.png") ' Crash occurs here. **************
I use standard built-in FileIO commands. I DO have some "Using" commands, but none of them are used to load the .png. I can't figure out how to release/dispose/close the process so the PNG can be deleted.
TIA.
-------------------------------------
RESOLVED:
Trying to Dispose the "img" AFTER it was assigned "nothing" was the problem. Deleting "img = Nothing" stops the "img.Dispose()" from causing a runtime error.
Last edited by Mugsy323; Oct 15th, 2024 at 08:23 AM.
-
Oct 15th, 2024, 08:21 AM
#2
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
You need to apply some logic to what you're doing. If you call Image.FromFile then you lock the file and it cannot be modified until you dispose the Image object, built disposing the Image object makes it unusable, so you can't do that until you have finished using it. You need to either not call Image.FromFile in the first place or else not try to delete the file(s) until you have fi8nished using the Image(s).
-
Oct 15th, 2024, 08:43 AM
#3
Thread Starter
Lively Member
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
Thanks. I understood that, but couldn't figure out why I couldn't dispose of it after use.
Turns out, assigning it to "Nothing" was a mistake.
-
Oct 15th, 2024, 09:24 PM
#4
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
You weren't "assigning it to Nothing". You were assigning Nothing to the variable. If a variable refers to an object and then you assign Nothing to that variable, the variable no longer refers to that object, so of course you can't then access the object via that variable in order to dispose it. You need to access the object via the variable while the variable refers to the object. It may make sense to then set the variable to Nothing, because that will remove that reference and make the object available for garbage collection. That's only worthwhile if the variable itself remains in scope for some time though. If it's a local variable then it will fall out of scope at the end of the block it was declared in and the reference will be removed then anyway.
In this specific case, part of the problem was that you didn't show the code that was an issue as it was an issue and you also didn't provide all the relevant information. You showed the code with that line commented out, which didn't make it clear that the preceding line was being used with it rather than instead of it. You also didn't mention that calling Dispose threw a NullReferenceException, which would have made it obvious what was happening.
Last edited by jmcilhinney; Oct 16th, 2024 at 07:57 PM.
-
Oct 16th, 2024, 05:27 AM
#5
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
There is an art to raising a bug report or a forum request for help.
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
Oct 16th, 2024, 10:27 AM
#6
Thread Starter
Lively Member
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
 Originally Posted by jmcilhinney
you also didn't provide all the relevant information. You showed the code with that line commented out, which didn't make it clear that the preceding line was being used with it rather than instead of it. You also didn't mention that calling Dispose threw a NullReferenceException, which would have made it obvious what was happening.
My apologies. If I understood the reasons behind the error, I probably would have provided more of the relevant code (total project much too big to repost.)
But, of course, if I understood the reasons behind the error, I wouldn't have needed help.
And fortunately, in the end, I didn't. Thx.
-
Oct 16th, 2024, 08:00 PM
#7
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
It's not about providing more code. It's about providing the actual code that generates the error and providing any and all error messages verbatim. The code you posted was not the actual code at issue because it had the problem line commented out and you also didn't provide the error message that that line would have generated and would have made the issue plain. You didn't need to understand the issue any better than you did to do that. It's not about understanding the problem. It's about understanding how to explain the problem.
-
Oct 17th, 2024, 07:08 AM
#8
Thread Starter
Lively Member
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
As I stated before, the entire project is much too large, and singling out snippets connected to the issue would have omitted so much code it would have been a disjointed mess.
The "commented out" line of code included a comment of it's own indicting that it was only added in a failed attempt to resolve this issue and was not part of the original problem.
The code I provided WAS sufficient to resolve the issue b/c I myself was able to spot my error and resolve the issue myself after posting it. I left the post up to aid anyone who might experience the same problem in the future.
I consider this matter closed.
-
Oct 17th, 2024, 07:19 AM
#9
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
Instead of insisting that you did nothing wrong, how about you listen to what people who read your post say it came across to people who read it? You might learn something that will help you write better questions in the future and increase the likelihood that you'll get the help you want. People who are determined that they never do anything wrong invariably continue to do the same things wrong. Anyway, if you don't think that the advice of someone who's been here for almost 20 years and answered more posts than anyone else is worth listening to, I guess I need to bow to your greater knowledge.
-
Oct 17th, 2024, 10:17 AM
#10
Thread Starter
Lively Member
Re: [RESOLVED] Can't delete file "in use by another process" (my own app). HELP!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|