BCB使用线程删除目录中的图片
BCB新建线程DeleteImgThread类。其会默认继承Thread类,然后在Execute函数中编写代码,
void __fastcall DeleteImgThread::Execute()
{
//---- Place thread code here ----
while(!this->Terminated)
{
//删除.\RecvTmp中的图片
AnsiString JepgDir = ExtractFilePath(ParamStr(0)) + "RecvTmp";
TSearchRec sr;
int iAttributes = faAnyFile;
if (FindFirst(JepgDir+ "\\*.jpg", iAttributes, sr) == 0)
{
do
{
TDateTime JepgTime = FileDateToDateTime(sr.Time);
if (Now()-1 > JepgTime)
{
DeleteFile(JepgDir + "\\"+ sr.Name);
}
}
while(FindNext(sr) == 0);
}
FindClose(sr);
Sleep(5000);
}
}
这里声明了一个系统结构体SearchRec变量sr,用于遍历目录,文件,与FindFirst、FindNext配合使用,使用系统基本函数DeleteFile()删除文件。注意sr使用完之后一定要FindClose(sr),不然会导致句柄不断添加。
一般使用系统变量的话都须要手动将其注销掉,不然会引起句柄不断添加。
创建好线程类之后,须要在主函数中声明调用
DeleteImgThread *DeleteImg; DeleteImg = new DeleteImgThread(NULL); // 清除曾经接收的图片new一个对象出来就一定要delete掉,切记!
//释放DeleteImgThread线程
if (DeleteImg)
{
DeleteImg->Terminate();
DeleteImg->Resume();
DeleteImg->WaitFor();
delete DeleteImg;
DeleteImg=NULL;
}
浙公网安备 33010602011771号