views:

33

answers:

2

I am working on VC++ project, in that my application process a file from input path and generates 3 output "*.DAT" files in the destination path. I will FTP these DAT file to the destination server. After FTP, I need to delete only two output .DAT files the folder. I am able to delete those files, because there one Asynchronous thread running behind the process. Since the thread is running, while deleting it says, "Cannot delete, the file is used by another person".

I need to stop that thread and delete the files. Multiple files can also be taken from the input path to process.

Please help me in resolving this issue. Its very high priority issue for me. Please help me ASAP.

A: 

I don't think this is a threading issue. Instead I think your problem is that Windows won't let you delete a file that still has open handles referencing it. Make sure that you call CloseHandle on handles to the file that you want to delete first. Also ensure that whatever mechanism you are using to perform the FTP transfer doesn't have any handles open to the file you want to delete.

torak
Hi torak, You are right, I haven't closed the handler before deleting the file. My problem has been resolved. Thank you.
JVNR
A: 

I don't think that forcing the background thread down will solve your problem. You can't delete the files because you're holding an open handle to those files. You must close the handle first. Create an event object and share it between your main thread and the background thread. When the background thread is done sending the files through FTP, it should set this event. Have your main thread wait on the event before deleting the files.

Background Thread:

SendFiles();
ReleaseResources(); // (might be necessary, depending on your design)
SetEvent( hFilesSentEvent );

Main Thread:

WaitForSingleObject( hFilesSentEvent );
DeleteFiles();
Peter Ruderman
Hi Peter,Thank you. I have closed the handler now and my application executes well now.
JVNR