Deleting Expired State Files
Because HTTP is stateless, Web applications have no real way of knowing when the user is finished using it. Because this sample application stores user state information in files, we can't rely on the user to tell the application when to delete its state file. Instead, we have to run some other process to delete any state file we think is no longer needed; that is, an expired state file.
Writing the VB code for a loop that deletes all expired state files is pretty simple. Here's an example:
Dim StateFileName As String Dim DateMod As Date StateFileName = Dir(App.Path & "\*.usf") Do Until StateFileName = "" If DateDiff("n", FileDateTime(StateFileName), Now) > 60 Then Call Kill(StateFileName) End If StateFileName = Dir Loop
Of course in the above code, the path for the state files, and possibly the expiration period, should be configurable from an INI file option or a registry setting. The code should also contain an appropriate error handler so that it won't crash if it has trouble killing a state file.
This little bit of code could be contained in the Web application itself and executed every time a user makes a request to the application. Depending on the number of concurrent users your site has, this approach could result in significantly increased response times. Instead, I recommend compiling this little bit of code into a stand-alone executable, and then using a scheduler program to periodically run it at a time when your server tends to have a low processing load.