Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > API > Closing all file handles


Reply
 
Thread Tools Display Modes
  #1  
Old 07-29-2010, 06:31 PM
Jason88 Jason88 is offline
Junior Contributor
 
Join Date: Oct 2005
Posts: 261
Default Closing all file handles

Hello everybody, is there an API to close all open file handles, similar to Reset (VB6)?

Right now I use CloseHandle, but often I'm writing multiple files at the same time. When the user closes the app while writing the files, the handles are not closed. The only way to do that, is to store the file handles in an array and loop through the array with CloseHandle in the Form_Unload event.

I could show a messagebox and tell the user there are still files being written and that the user needs to wait, but in this case I rather close all files. I'm just looking for an easier way.
Reply With Quote
  #2  
Old 07-29-2010, 10:50 PM
vb5prgrmr vb5prgrmr is offline
Senior Contributor

* Expert *
 
Join Date: Mar 2009
Posts: 1,093
Default

Usually, and I do mean usually,... file operations are done all at once, meaning, open>write>close and your done! Even if you do have some sort of auto saving feature...

But with that said, there are those applications that need to keep a file open and when this is the case as it seems to be in your case, you need to program defensively and that does mean that if you are closing down unexpectedly, you need to take care of all your handles, connections, and whatever other external or internal references you have open...



Good Luck
__________________
Just because I'm an expert does not mean I know it all and just because I know it all does not mean I'm an expert
Reply With Quote
  #3  
Old 07-29-2010, 11:19 PM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 5,943
Default

Just wondering what your app is actually doing that you would be doing a significant amount of writing to files (?asynchronously, or in another thread??) that you couldn't just 'handle' during shutdown requests (i.e. acknowledge but stall shutdown until you are done writing to the files.

Can you give some more details regarding your application and how you are writing to these files? There might be a simpler and cleaner solution we can suggest
__________________
I recommend you read something else as this sentence is now concluded.
Reply With Quote
  #4  
Old 07-30-2010, 08:03 AM
Jason88 Jason88 is offline
Junior Contributor
 
Join Date: Oct 2005
Posts: 261
Default

My app is downloading files with Winsock and writing them to disk. I'm using CreateFile/WriteFile API to write the files, because I like it better than Open/Put.

Actually the file writing is done with a Usercontrol array, so after thinking about it, I don't need to store the file handles in an array. The only thing I have to do is add a sub to the Usercontrol to close the handle and then loop through the usercontrol array and calling the sub the in the Form_Unload event.

Code:
Public Sub StopWriting()
    CloseHandle File_Number
End Sub
Code:
Private Sub Form_Unload(Cancel As Integer)
    Dim i As Long
    
    For i = 0 To FileWrite.UBound
        FileWrite(i).StopWriting
    Next
End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb