-
Aug 19th, 2011, 07:07 PM
#1
Thread Starter
New Member
[RESOLVED] Process cannot access a file because it is being used by another process
Hi,
I frequently encounter “Process cannot access a file because it is being used by another process” error when trying to perform a file function (eg delete) that requires the Access Database file to be closed.
I open the database file in exclusive user mode as follows:
Dim ltStr As String
ltStr = "Provider=Microsoft.Jet.OLEDB.4.0; "
ltStr &= " Data Source=" + gtPath + "\abc.mdb; "
ltStr &= " Mode = Share Deny Read |Share Deny Write; "
ltStr &= " Jet OLEDBatabase Password= '" & gtPassWord & "'"
oCon = New OleDbConnection(ltStr)
oCon.Open()
I attempt to delete the file with the following code :
oCon.Close()
oCon.Dispose()
System.IO.File.Delete("abc.mdb")
This works only about 5% of the time – 95% crashing with the process error described above.
I can dramatically improve the success rate to about 70% using the following code to try and close / dispose the connection multiple times
Do
TryAgain:
Try
System.IO.File.Delete("abc.mdb")
Catch ex As Exception
oCon.Close()
oCon.Dispose()
liCount = liCount + 1
If liCount > 10 Then
li = MsgBox("Can not Delete Files as they are still being used" & Chr(10) & "Do you wish to try again", MsgBoxStyle.YesNo, "Deletion Failed")
If li = 6 Then
GoTo TryAgain
Else
Exit Sub
End If
End Try
Application.DoEvents()
Loop
Is there anyway I can force the immediate closure of the connection so as to ensure the deletion works 100% of the time?
I am only opening the connection once at the start of the program.
Is this wrong – should I be opening / closing the connection at the start and end of each database query and update
-
Aug 20th, 2011, 11:14 AM
#2
Re: Process cannot access a file because it is being used by another process
Is this a single user application ?
Are you viewing the mdb file using Access when you run the app ?
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
-
Aug 20th, 2011, 03:02 PM
#3
Re: Process cannot access a file because it is being used by another process
It seems strange that if you close the database as soon as you are done with it then sometime goes by you attempt to remove it and fails says to me the database connection is still open or (not knowing your environment) another process has the database open
While the MDB is open there will be a lock file present, until it goes away you cannot remove the MDB.
Before you attempt to remove the MDB try the following. When trying the following out make sure you do not have MS-Access application open.
Code:
Msgbox("Waiting")
System.IO.File.Delete("abc.mdb")
When the message box appears traverse to the folder with abc.mdb and see if abc lock file exists which is the lock file. If the lock file exists then the delete will fail while if the lock file is not there you can delete the file.
One possible solution would be to go into a loop to remove the file. The intent is to allow time for the file to close. When trying the following out make sure you do not have MS-Access application open.
Example
Code:
''' <summary>
''' Remove MS-Access database
''' </summary>
''' <param name="FileName">Database to remove</param>
''' <param name="WaitTime">Sleep time</param>
''' <param name="Loops">How many attempts to try</param>
''' <remarks></remarks>
Function RemoveAccessDatabase( _
ByVal FileName As String, _
ByVal WaitTime As Integer, _
ByVal Loops As Integer) As Boolean
Dim Success As Boolean = False
Dim LockFile As String = IO.Path.ChangeExtension(FileName, "ldb")
For Counter As Integer = 0 To Loops
If IO.File.Exists(LockFile) Then
System.Threading.Thread.Sleep(WaitTime)
IO.File.Delete(FileName)
Else
Success = True
Exit For
End If
Next
Return Success
End Function
Now call it as follows
Code:
oCon.Close()
RemoveAccessDatabase("abc.mdb", 100, 5)
Let's say that fails, increase the second param in 100 increments until it does work.
On a different topic you need to avoid Go To statements as they can cause more problems then they are worth. Learn how to code without them.
Last edited by kareninstructor; Aug 20th, 2011 at 03:07 PM.
Reason: Wording
-
Aug 20th, 2011, 07:04 PM
#4
Thread Starter
New Member
Re: Process cannot access a file because it is being used by another process
Originally Posted by jggtz
Is this a single user application ?
Are you viewing the mdb file using Access when you run the app ?
Yes this is a single user application.
The application reads and writes to the MDB file but these operations are completed well before trying to delete the file.
There appears for some unknown reason a delay in closing the connection.
-
Aug 20th, 2011, 07:16 PM
#5
Thread Starter
New Member
Re: Process cannot access a file because it is being used by another process
Originally Posted by kevininstructor
It seems strange that if you close the database as soon as you are done with it then sometime goes by you attempt to remove it and fails says to me the database connection is still open or (not knowing your environment) another process has the database open
While the MDB is open there will be a lock file present, until it goes away you cannot remove the MDB.
Before you attempt to remove the MDB try the following. When trying the following out make sure you do not have MS-Access application open.
Code:
Msgbox("Waiting")
System.IO.File.Delete("abc.mdb")
When the message box appears traverse to the folder with abc.mdb and see if abc lock file exists which is the lock file. If the lock file exists then the delete will fail while if the lock file is not there you can delete the file.
One possible solution would be to go into a loop to remove the file. The intent is to allow time for the file to close. When trying the following out make sure you do not have MS-Access application open.
Example
Code:
''' <summary>
''' Remove MS-Access database
''' </summary>
''' <param name="FileName">Database to remove</param>
''' <param name="WaitTime">Sleep time</param>
''' <param name="Loops">How many attempts to try</param>
''' <remarks></remarks>
Function RemoveAccessDatabase( _
ByVal FileName As String, _
ByVal WaitTime As Integer, _
ByVal Loops As Integer) As Boolean
Dim Success As Boolean = False
Dim LockFile As String = IO.Path.ChangeExtension(FileName, "ldb")
For Counter As Integer = 0 To Loops
If IO.File.Exists(LockFile) Then
System.Threading.Thread.Sleep(WaitTime)
IO.File.Delete(FileName)
Else
Success = True
Exit For
End If
Next
Return Success
End Function
Now call it as follows
Code:
oCon.Close()
RemoveAccessDatabase("abc.mdb", 100, 5)
Let's say that fails, increase the second param in 100 increments until it does work.
On a different topic you need to avoid Go To statements as they can cause more problems then they are worth. Learn how to code without them.
Hi,
Thanks for the suggestion. I will certainly give it a try.
The problem however is that the required waittime varies each time I run the application in the development time. I will need to use a very pessimistic time in a run time enviroment.
What I was really hoping for is an instruction to kill the connection whether it is open and/or in use.
-
Aug 20th, 2011, 08:52 PM
#6
Re: Process cannot access a file because it is being used by another process
Originally Posted by Jeff Las
Hi,
Thanks for the suggestion. I will certainly give it a try.
The problem however is that the required waittime varies each time I run the application in the development time. I will need to use a very pessimistic time in a run time enviroment.
What I was really hoping for is an instruction to kill the connection whether it is open and/or in use.
First off there is no "unknown reason" for the connection to stay open. I would suggest the following if you tried my other suggestion.
Get rid of the current connection object and replace with the following
Code:
Dim cb As New OleDbConnectionStringBuilder
cb.Provider = "Microsoft.Jet.OLEDB.4.0"
cb.DataSource = IO.Path.Combine(gtPath, "abc.mdb")
cb("Mode") = "Share Deny Read |Share Deny Write"
cb("Jet OLEDBatabase Password") = gtPassWord
Using oCon As New OleDbConnection(cb.ConnectionString)
'
' Do what you came to do
'
End Using
After the Using statement code has completed the connection is then closed. If for some reason the database is still open it is because of something else has it open.
-
Aug 21st, 2011, 02:28 AM
#7
Fanatic Member
Re: Process cannot access a file because it is being used by another process
i'd try adding a
vb Code:
Threading.Thread.Sleep(1000)
before the file delete.
-
Aug 21st, 2011, 06:30 AM
#8
Re: Process cannot access a file because it is being used by another process
Originally Posted by TCarter
i'd try adding a
vb Code:
Threading.Thread.Sleep(1000)
before the file delete.
What happened?
-
Aug 23rd, 2011, 12:23 AM
#9
Member
Re: Process cannot access a file because it is being used by another process
I use the following code, which is generally successful:
Code:
Function SafeFileDelete(ByVal FileName As String) As Boolean
If My.Computer.FileSystem.FileExists(FileName) Then
For i As Integer = 1 To 10
Try
My.Computer.FileSystem.DeleteFile(FileName)
Exit For
Catch ex As Exception
If i = 10 Then
AddToLog("Unable to delete file " & FileName & " after 10 tries over 10 seconds. Exception says " & ex.Message)
Else
MySleep(1) ' same as Threading.Thread.Sleep(1000)
End If
End Try
Next
End If
Return My.Computer.FileSystem.FileExists(FileName)
End Function
I have this function running as part of a project that goes all day long, and maybe once an hour this code fails & I see in the log that the file was not in fact deleted, always due to the "Process cannot access a file because it is being used by another process" exception.
What I'm puzzled by, is WHAT other process is doing this? I'd really like to capture this info in the log, since the larger program needs to run continuously (it tolerates the failed-to-delete fault), rather than stopping to wait while I troubleshoot (hoping that the "other process" doesn't let go before I get to the keyboard). Can anybody point me to some code that will allow the program to determine what the "other process" is?
Thanks!
-
Aug 23rd, 2011, 01:52 AM
#10
Fanatic Member
Re: Process cannot access a file because it is being used by another process
Originally Posted by kevininstructor
What happened?
eh? not my thread.. was offering a suggestion.
-
Aug 23rd, 2011, 02:33 AM
#11
Thread Starter
New Member
Re: Process cannot access a file because it is being used by another process
Hi,
Thanks to all who have replied.
I am using KevinInstructors Removeaccessdatabase function as a quick fix - it is 90% successful.
I will be trying his alternative solution when I have some free time.
Thanks
-
Aug 23rd, 2011, 06:07 AM
#12
Re: [RESOLVED] Process cannot access a file because it is being used by another proce
Since you still have problems I would recommend downloading Process Monitor which by default monitors all applications running but you can have PM monitor just your application which should point you to the problem. Take time to learn how to filter what to monitor besides just your application such as filter out registry monitoring, if you do not you will have a lot to wad thru.
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
|