是这样的,我写一段代码,想实现FTP文件下载功能,我使用了线程,再5分钟内等待文件下载,如果超时,我就关闭打开的internet据柄,网络正常的时候下载是成功的,但是当网络不好的时候,5分钟内没下载成功,我用把下载线程结束啦,重新连接网络,可是这时候下载总是不成功,返回的消息是“The process cannot access the file because it is being used by another process.”请问,这是怎么回事。急!!

解决方案 »

  1.   

    你用什么方式写ftp程序的? wininet还是socket?下载时时候有在程序里Create一个file?
    你查看一下所有打开的文件是否都有被Close了
      

  2.   

    int GetFileA(CString strRemoteFilePath, CString strLocalFilePath,int type)
    {
    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;
    }
      

  3.   

    建议不要用TerminateThread(hThread,dwExitCode); 很不安全,线程结束资源没有释放很可能是因为它
      

  4.   

    那有好的结束线程的API马?我用ExitThread()不行,他会把整个程序结束掉。
      

  5.   

    设置全局变量
    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;
    }
      

  6.   

    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 = FALSE;
    CloseHandle(hThread);
    return OPR_TIMEOUT;
    }
    写错了:)