开发者

VC++ - Asynchronous Thread

开发者 https://www.devze.com 2022-12-29 03:59 出处:网络
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.

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.


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.


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消