是这样的,我写一段代码,想实现FTP文件下载功能,我使用了线程,再5分钟内等待文件下载,如果超时,我就关闭打开的internet据柄,网络正常的时候下载是成功的,但是当网络不好的时候,5分钟内没下载成功,我用把下载线程结束啦,重新连接网络,可是这时候下载总是不成功,返回的消息是“The process cannot access the file because it is being used by another process.”请问,这是怎么回事。急!!
调试欢乐多
你查看一下所有打开的文件是否都有被Close了
{
COPYFILE stCopyFile;
HANDLE hThread;
DWORD dwThreadID;
DWORD dwRnt;
DWORD dwExitCode;
DWORD dwtimeout; stCopyFile.strSrc = strRemoteFilePath;
stCopyFile.strDist = strLocalFilePath; dwtimeout = 300*INTERNET_TIMEOUT; // 5分 hThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadGetFile,
&stCopyFile,
0,
&dwThreadID);
dwRnt = WaitForSingleObject(hThread,dwtimeout);
if(WAIT_OBJECT_0 == dwRnt )
{
//retrieves the termination status of the specified thread
GetExitCodeThread(hThread,&dwExitCode);
CloseHandle(hThread);
return dwExitCode;
}
else
{
dwExitCode = (DWORD)OPR_FAILE;
// terminates a thread
TerminateThread(hThread,dwExitCode);
CloseHandle(hThread);
return OPR_TIMEOUT;
}}int ThreadGetFile(LPVOID vThreadP)
{
COPYFILE *stCopyFilePath = (COPYFILE *)vThreadP;
CString strLocalFilePath,strRemoteFilePath;
int iRetryNum=0; strLocalFilePath = stCopyFilePath->strDist;
strRemoteFilePath = stCopyFilePath->strSrc; while(!FtpGetFile(m_hFtpSession,
strRemoteFilePath,
strLocalFilePath,
FALSE,
FILE_ATTRIBUTE_ARCHIVE,
INTERNET_FLAG_DONT_CACHE|FTP_TRANSFER_TYPE_ASCII,
0))
{
iRetryNum++;
if(iRetryNum > RETRYMAX)
{ return OPR_FAILE;
}
}
return OPR_SUCC;
}
while(g_bContinue && !FtpGetFile(m_hFtpSession,
strRemoteFilePath,
strLocalFilePath,
FALSE,
FILE_ATTRIBUTE_ARCHIVE,
INTERNET_FLAG_DONT_CACHE|FTP_TRANSFER_TYPE_ASCII,
0))
{
iRetryNum++;
if(iRetryNum > RETRYMAX)
{ return OPR_FAILE;
}
}
dwRnt = WaitForSingleObject(hThread,dwtimeout);
if(WAIT_OBJECT_0 == dwRnt )
{
//retrieves the termination status of the specified thread
GetExitCodeThread(hThread,&dwExitCode);
CloseHandle(hThread);
return dwExitCode;
}
else
{
dwExitCode = (DWORD)OPR_FAILE;
// terminates a thread
g_bContinue = TRUE;
CloseHandle(hThread);
return OPR_TIMEOUT;
}
if(WAIT_OBJECT_0 == dwRnt )
{
//retrieves the termination status of the specified thread
GetExitCodeThread(hThread,&dwExitCode);
CloseHandle(hThread);
return dwExitCode;
}
else
{
dwExitCode = (DWORD)OPR_FAILE;
// terminates a thread
g_bContinue = FALSE;
CloseHandle(hThread);
return OPR_TIMEOUT;
}
写错了:)