Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp process not releasing files

Status
Not open for further replies.

bbxrider

Programmer
Sep 23, 2008
18
US
my page is form, that creates 2 files when processing the submitted data. the files are closed and their objects are set to nothing and the very last of the process is a page redirect. however after that, for instance i cannot delete the files, because they are still being used somehow by the asp process, the one that was created when the page was initially invoked.

see following for file code, its the same for both files:

Dim vcf As System.IO.StreamWriter
Try
vcf = System.IO.File.AppendText(appsPath & firstName.Text & "." & lastName.Text & ".vcf")
etc, etc.
vcf.close()
vcf = Nothing

the process holding the files is aspnet.exe, if i try to delete, i get the windows msg, 'cannot delete..... It is being used by another person or program. Close any programs......'

i see in the default vs2005 global.asax file there is a comment for the session_end, see below. is that where i need to put some code to fully and finally end that particular page instance process? if so, not sure what is really needed there?

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)

' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
End Sub

The process also opens, updates and closes an access db. it is fully released when the process ends, so at least that is working ok
 
you need to dispose of the stream, not just close it and set it to nothing. you also need to dispose of it even if an exception is thrown, so a using block is the best way to handle this.
Code:
using(var writer = File.AppendText("file path"))
{
   writer.Write(...);
   writer.Close();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
so far not following this, it looks like your saying to put all of it, the open close, etc, inside a 'using' block and that will make the difference?
and if its in a using block, you don't have to explicitly declare the streamwriter variable?

even though i currently invoke a close and set to nothing, somehow that doesn't dispose of the 'stream' the same way closing and setting to nothing does say with an oledb connection?

i have a 3rd file, a log, that i have coded the same as the other 2 and it gets released. the only differnce is the variable for the stream writer (Dim lg As System.IO.StreamWriter) is declared at the script/module level whereas the 2 problem files have their variable declared inside a function.
 
the streamwriter variable has been declared by using var (though that depends on the version of the .Net framework you are using)

var is a keyword that basically tells the compiler to decide the best type to use in this instance.

In a nutshell the using block will correclty close and dispose of objects it is 'using'.

 
it looks like your saying to put all of it, the open close, etc, inside a 'using' block and that will make the difference?
yes
and if its in a using block, you don't have to explicitly declare the streamwriter variable?
no, var is syntax sugar in .net 3.0+
even though i currently invoke a close and set to nothing, somehow that doesn't dispose of the 'stream' the same way closing and setting to nothing does say with an oledb connection?
you understanding of close/nothing for database connections is incorrect. you should be disposing of the object. so rather than your file streams following your current db configuration. your db configuration should follow the dispose pattern.
i have a 3rd file, a log, that i have coded the same as the other 2 and it gets released.
this may be a timing issue, or the log is not written to, so the stream is not opened.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
unfortunately i'll have to try these changes later, hope you guys are still around then. i'm using vs2005 and .net 2.0.50727.3082
the log file is written to
just real quick i tried a
Code:
using (var vcf=File.AppendText(appsPath & "\" & firstName.Text & "." & lastName.Text & ".vcf"))
and vs didn't like that
also saw there is a object.Dispose() that could be coded to achieve the same as the 'using' block invoking that?
 
Code:
using(IDisposable foo = new DisposableObject())
{
   //use foo
}
is syntax sugar for
Code:
IDisposable foo;
try
{
   foo = new DisposableObject();
   //use foo
}
finally
{
   if(foo != null)
   {
      foo.Dispose();
   }
}
as for the "var" keyword. just replace it with an explicit type and you should be good.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top