读书人

关于CFtpConnection同时下载文件的有关

发布时间: 2012-01-18 00:23:26 作者: rapoo

关于CFtpConnection同时下载文件的问题

C/C++ code
#define DL_BUFFER_SIZE 4096#define PROGRESS_REPORT WM_USER + 1BOOL CFtpClient::Download(CString sourcename, CString destpath, HWND hwnd){    CString sourcepath;    BOOL bl = FindFile(sourcename, sourcepath);    if (bl == FALSE)    {        return FALSE;    }    CInternetFile*  FtpFile = m_pFtpCon->OpenFile(sourcepath);    CFile LocalFile;    if(LocalFile.Open(destpath, CFile::modeCreate | CFile::modeWrite, NULL) == FALSE)    {        return FALSE;    }    char buffer[DL_BUFFER_SIZE];    unsigned int amount_read = DL_BUFFER_SIZE;    unsigned int total_read = 0;    while (amount_read == DL_BUFFER_SIZE)    {        amount_read = FtpFile->Read(buffer, DL_BUFFER_SIZE);        LocalFile.Write(buffer, amount_read);        PostMessage(hwnd, PROGRESS_REPORT, 0, 0);        //给调用窗口发进度消息    }    FtpFile->Close();    LocalFile.Close();    return TRUE;}



这是我的下载函数 用按钮控制启动线程下载 问题是当一个文件没有下载完成 也就是FtpFile->Close()没执行的时候,再点击按钮开线程下载会导致m_pFtpCon->OpenFile(sourcepath)异常。
我目前的解决办法是每次开线程都重新
m_pFtpCon = m_InternetSession->GetFtpConnection(ip, user, passwd, port);一次,这样可以解决问题。
但是不知道好不好。请问有别的方法实现吗?FTP客户端软件又是如何实现的呢?

[解决办法]
那你就全部用栈变量吧,不要使用成员变量了,否则需要加入线程保护的。

读书人网 >VC/MFC

热点推荐