Search code examples
.netvb.nettext-filesfile-accessread-write

Error: File it is being used by another process, even if closed and disposed


I'm having problems to access a file.

Every 1 second (more or less) my application is calling this procedure to download a sourcecodepage and save it in a textfile, the problem is sometimes when is called too much for example more than 50 times I get the error:

"Can't access file, file it is being used by another process"

I've tried to close it and dispose it, but nothing.

EDIT: The file who is causing the error is "OutPutFile" Variable which is a file in %TEMP% dir.

' Get URL SourceCode
Function Get_URL_SourceCode(ByVal url As String, Optional ByVal OutputFile As String = Nothing, Optional ByVal delimit As Boolean = False) As String

    Dim SourceCode As String = String.Empty

    If Want_to_cancel_thread Then Return Nothing

    Try
        Dim request As HttpWebRequest = HttpWebRequest.Create(url)
        Dim response As HttpWebResponse = request.GetResponse()
        Dim sr As StreamReader = New StreamReader(response.GetResponseStream())
        SourceCode = sr.ReadToEnd()
        sr.Close() : sr.Dispose()

        If delimit Then SourceCode = Delimit_String(SourceCode, "<div id=""centercol"">", "<!--/centercol -->")

        If OutputFile IsNot Nothing Then

            'File.Open(OutputFile, FileMode.CreateNew, FileAccess.Write, FileShare.Write)
            'Using TextFile As New StreamWriter(OutputFile, False, Encoding.Default)
            '    TextFile.WriteLine(SourceCode)
            'End Using
            File.WriteAllText(OutputFile, SourceCode, Encoding.Default)
        End If

    Catch ex As Exception
        MsgBox("Error: " & ex.Message)
    End Try

    Return SourceCode

End Function

Solution

  • Try using SyncLock so the working thread locks the object and no other process access the file:

    'Global variable lock
    
    Dim objectLock As Object = New Object
    
    
    Function Get_URL_SourceCode(ByVal url As String, Optional ByVal OutputFile As String = Nothing, Optional ByVal delimit As Boolean = False) As String
    
     Dim SourceCode As String = String.Empty
    
     'Lock object
     SyncLock objectLock 
    
     If Want_to_cancel_thread Then Return Nothing
    
        Try
            Dim request As HttpWebRequest = HttpWebRequest.Create(url)
            Dim response As HttpWebResponse = request.GetResponse()
            Dim sr As StreamReader = New StreamReader(response.GetResponseStream())
            SourceCode = sr.ReadToEnd()
            sr.Close() : sr.Dispose()
    
            If delimit Then SourceCode = Delimit_String(SourceCode, "<div id=""centercol"">", "<!--/centercol -->")
    
            If OutputFile IsNot Nothing Then
    
                'File.Open(OutputFile, FileMode.CreateNew, FileAccess.Write, FileShare.Write)
                'Using TextFile As New StreamWriter(OutputFile, False, Encoding.Default)
                '    TextFile.WriteLine(SourceCode)
                'End Using
                File.WriteAllText(OutputFile, SourceCode, Encoding.Default)
            End If
    
        Catch ex As Exception
            MsgBox("Error: " & ex.Message)
        End Try
    
        'End lock
        End SyncLock
    
        Return SourceCode
    
     End Function