R
Rob
This is a weird one... I've got a class called PageInfo that has the
following finalize code:
Protected Overrides Sub Finalize()
MyBase.Finalize()
Do While m_TempFolders.Count
Dim TempPath As String = m_TempFolders.Dequeue
Granite.DeleteFolder(TempPath)
Loop
End Sub
The Granite.DeleteFolder routine is show belown. Basically, the class uses
some temporary folders. When the class creates them, it sticks the path in
the m_TempFolders queue. When the class is destroyed by the garbage
collector, the Finalize routine is called and deletes the temporary folder.
The way the program works is that upto ten PageInfo objects are kept in a
stack and when the 10th one is added, the one out of the other end of the
stack is deleted. This causes the finalize routine to be called shortly
afterwards.
The temporary folder is getting deleted okay BUT the session terminates -
the ASP.NET v2 Session_End event first, followed shortly by the
Session_Start.
Is it legal to call code like in DeleteFolder from within Finalize?
Thanks, Rob.
' DeleteFolder: Removes a folder and *all* the contents. USE WITH CARE!!
Public Sub DeleteFolder(ByVal Path As String)
If System.IO.Directory.Exists(Path) Then
For Each File As String In System.IO.Directory.GetFiles(Path)
Try
System.IO.File.SetAttributes(File,
IO.FileAttributes.Normal)
System.IO.File.Delete(File)
Catch ex As Exception
' Ignore delete error
End Try
Next
For Each Directory As String In
System.IO.Directory.GetDirectories(Path)
DeleteFolder(Directory)
Next
Try
System.IO.Directory.Delete(Path)
Catch ex As Exception
' Ignore delete folder error
End Try
End If
End Sub
following finalize code:
Protected Overrides Sub Finalize()
MyBase.Finalize()
Do While m_TempFolders.Count
Dim TempPath As String = m_TempFolders.Dequeue
Granite.DeleteFolder(TempPath)
Loop
End Sub
The Granite.DeleteFolder routine is show belown. Basically, the class uses
some temporary folders. When the class creates them, it sticks the path in
the m_TempFolders queue. When the class is destroyed by the garbage
collector, the Finalize routine is called and deletes the temporary folder.
The way the program works is that upto ten PageInfo objects are kept in a
stack and when the 10th one is added, the one out of the other end of the
stack is deleted. This causes the finalize routine to be called shortly
afterwards.
The temporary folder is getting deleted okay BUT the session terminates -
the ASP.NET v2 Session_End event first, followed shortly by the
Session_Start.
Is it legal to call code like in DeleteFolder from within Finalize?
Thanks, Rob.
' DeleteFolder: Removes a folder and *all* the contents. USE WITH CARE!!
Public Sub DeleteFolder(ByVal Path As String)
If System.IO.Directory.Exists(Path) Then
For Each File As String In System.IO.Directory.GetFiles(Path)
Try
System.IO.File.SetAttributes(File,
IO.FileAttributes.Normal)
System.IO.File.Delete(File)
Catch ex As Exception
' Ignore delete error
End Try
Next
For Each Directory As String In
System.IO.Directory.GetDirectories(Path)
DeleteFolder(Directory)
Next
Try
System.IO.Directory.Delete(Path)
Catch ex As Exception
' Ignore delete folder error
End Try
End If
End Sub