Application not letting go of file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Cooper

    Application not letting go of file.

    I posted this before but apparently it was eaten by the great Internet gods.


    I've got an application that watches a file. When that file is changed
    the application reads the data on the file and does stuff with it. In
    the application I have the following code in an event.

    ....
    ....
    Dim fs As FileStream
    Dim sr As StreamReader

    <do stuff here>

    fs = File.OpenRead(p ath)
    sr = New StreamReader(fs )

    <do stuff here>

    fs = Nothing
    sr = Nothing

    End Sub


    The first time the file is changed, the code works fine. The problem is
    that after the first time the file never gets released by my application
    so nothing else can actually change the file because it "is currently
    being used by another program." So, the program works once and then the
    file can't get changed again. That's not useful, obviously.

    Is there some way to release the file that I'm not aware of? I thought
    setting the Stream and the Reader to Nothing would do the trick and I
    don't see a method on either of those to actually let go of the file. I
    even tried File.Delete(pat h) just for grins and giggles but that gives
    me the "File is currently being used by another program." too.

    Any help would be greatly appreciated.

    Andrew Cooper
  • Tom Shelton

    #2
    Re: Application not letting go of file.

    On 2008-06-26, Andrew Cooper <kairoscreative @gmail.comwrote :
    I posted this before but apparently it was eaten by the great Internet gods.
    >
    >
    I've got an application that watches a file. When that file is changed
    the application reads the data on the file and does stuff with it. In
    the application I have the following code in an event.
    >
    ...
    ...
    Dim fs As FileStream
    Dim sr As StreamReader
    >
    ><do stuff here>
    >
    fs = File.OpenRead(p ath)
    sr = New StreamReader(fs )
    >
    ><do stuff here>
    >
    fs = Nothing
    sr = Nothing
    >
    End Sub
    >
    >
    You need to call close (or dispose) on your streams. Setting them to
    nothing is actually an exercise in futility - especially if they are
    local variables.

    You don't really need to use the FileStream in what I'm seeing there, but
    doing the following should fix your problem example:

    ' sr is local to the block, and automatically cleaned up at the end
    Using sr As New StreamReader (File.OpenRead (path))
    ' do stuff
    End Using

    The other option is of course, to simply call close:

    ' do stuff
    fs.Close()
    sr.Close()

    --
    Tom Shelton

    Comment

    • kimiraikkonen

      #3
      Re: Application not letting go of file.

      On Jun 26, 10:31 pm, Andrew Cooper <kairoscreat... @gmail.comwrote :
      I posted this before but apparently it was eaten by the great Internet gods.
      >
      I've got an application that watches a file. When that file is changed
      the application reads the data on the file and does stuff with it. In
      the application I have the following code in an event.
      >
      ...
      ...
      Dim fs As FileStream
      Dim sr As StreamReader
      >
      <do stuff here>
      >
      fs = File.OpenRead(p ath)
      sr = New StreamReader(fs )
      >
      <do stuff here>
      >
      fs = Nothing
      sr = Nothing
      >
      End Sub
      >
      The first time the file is changed, the code works fine. The problem is
      that after the first time the file never gets released by my application
      so nothing else can actually change the file because it "is currently
      being used by another program." So, the program works once and then the
      file can't get changed again. That's not useful, obviously.
      >
      Is there some way to release the file that I'm not aware of? I thought
      setting the Stream and the Reader to Nothing would do the trick and I
      don't see a method on either of those to actually let go of the file. I
      even tried File.Delete(pat h) just for grins and giggles but that gives
      me the "File is currently being used by another program." too.
      >
      Any help would be greatly appreciated.
      >
      Andrew Cooper
      As Tom mentioned, please use "Using" statements to make sure all the
      resources and also underlying resources are released. You can use
      using with no hesitation with the classes that implement IDisposable.

      Thanks,

      Onur Güzel

      Comment

      • Phill W.

        #4
        Re: Application not letting go of file.

        Andrew Cooper wrote:

        This is a classic VB "Proper" Developers' misunderstandin g - I've done
        it myself once or twice ;-)

        Setting Object [Reference] Variables to Nothing /rarely/ has any useful
        effect. You're simply telling the run-time:
        "I don't care what happens to this object any more. Please get rid of
        it (i.e. Garbage Collect it) sometime, if and when you get around to it".

        To kill off the "resources" (here, that's your file) that an object is
        holding on to, you have to explicitly Dispose of it. Have a read up on
        the IDisposable pattern (or Interface).

        Revisiting your code:

        ' Explicitly initialise variable; avoids warnings in VS'2005.
        Dim fs As FileStream = Nothing
        Dim sr As StreamReader = Nothing
        .. . .
        Try
        fs = File.OpenRead( path )
        sr = New StreamReader( fs )
        .. . .
        sr.Close() ' implicitly Dispose's the underlying stream

        Catch ex as Exception

        If Not ( sr Is Nothing ) then
        sr.Close()
        ElseIf Not ( fs Is Nothing ) then
        fs.Dispose()
        End If

        End Try

        You can also use the "Using ... End Using" construct, which implicitly
        does the Dispose (coded long-hand above) for you.

        HTH,
        Phill W.

        Comment

        • Andrew Cooper

          #5
          Re: Application not letting go of file.

          Phill W. wrote:
          Andrew Cooper wrote:
          >
          This is a classic VB "Proper" Developers' misunderstandin g - I've done
          it myself once or twice ;-)
          >
          Setting Object [Reference] Variables to Nothing /rarely/ has any
          useful effect. You're simply telling the run-time:
          "I don't care what happens to this object any more. Please get rid of
          it (i.e. Garbage Collect it) sometime, if and when you get around to it".
          >
          To kill off the "resources" (here, that's your file) that an object is
          holding on to, you have to explicitly Dispose of it. Have a read up
          on the IDisposable pattern (or Interface).
          >
          Revisiting your code:
          >
          ' Explicitly initialise variable; avoids warnings in VS'2005.
          Dim fs As FileStream = Nothing
          Dim sr As StreamReader = Nothing
          . . .
          Try
          fs = File.OpenRead( path )
          sr = New StreamReader( fs )
          . . .
          sr.Close() ' implicitly Dispose's the underlying stream
          >
          Catch ex as Exception
          >
          If Not ( sr Is Nothing ) then
          sr.Close()
          ElseIf Not ( fs Is Nothing ) then
          fs.Dispose()
          End If
          >
          End Try
          >
          You can also use the "Using ... End Using" construct, which implicitly
          does the Dispose (coded long-hand above) for you.
          >
          HTH,
          Phill W.
          Phil,

          You are DA man. I knew it was something stupid I was missing. I even
          looked for a Close-esque method on fs but didn't see anything. I was
          looking at the wrong object. Thanks for setting me straight!

          Andrew

          Comment

          • Michel Posseth  [MCP]

            #6
            Re: Application not letting go of file.

            Phill it wil also take care of the Explicitly initialise variable nag
            warning of VS

            Nowadays i code all my constructs like this as it

            1. doesn`t give you the nag warnings and thus the need to set a nohing /
            empty pointer
            2. it is shorter to type
            3. gives you extra scope in long routines ( variabels declared in using
            blocks are only in scope within the using block )
            4. it takes care of cleanup
            5. it just looks nicer to me

            Try
            using fs As FileStream = File.OpenRead( path )
            using sr As StreamReader = New StreamReader( fs )

            end using
            end using

            Catch ex as Exception

            End Try

            regards

            Michel



            "Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kschreef in bericht
            news:g42dmk$hsu $1@south.jnrs.j a.net...
            Andrew Cooper wrote:
            >
            This is a classic VB "Proper" Developers' misunderstandin g - I've done it
            myself once or twice ;-)
            >
            Setting Object [Reference] Variables to Nothing /rarely/ has any useful
            effect. You're simply telling the run-time:
            "I don't care what happens to this object any more. Please get rid of it
            (i.e. Garbage Collect it) sometime, if and when you get around to it".
            >
            To kill off the "resources" (here, that's your file) that an object is
            holding on to, you have to explicitly Dispose of it. Have a read up on
            the IDisposable pattern (or Interface).
            >
            Revisiting your code:
            >
            ' Explicitly initialise variable; avoids warnings in VS'2005.
            Dim fs As FileStream = Nothing
            Dim sr As StreamReader = Nothing
            . . .
            Try
            fs = File.OpenRead( path )
            sr = New StreamReader( fs )
            . . .
            sr.Close() ' implicitly Dispose's the underlying stream
            >
            Catch ex as Exception
            >
            If Not ( sr Is Nothing ) then
            sr.Close()
            ElseIf Not ( fs Is Nothing ) then
            fs.Dispose()
            End If
            >
            End Try
            >
            You can also use the "Using ... End Using" construct, which implicitly
            does the Dispose (coded long-hand above) for you.
            >
            HTH,
            Phill W.

            Comment

            Working...