問題貌似出現在 AfxBeginThread(mtQuery, pParam, THREAD_PRIORITY_NORMAL, 0,NULL);
這一行代碼中。函數 mtQuery 如下。
為什麼會出現上面圖片那一行的錯誤。。
#ifndef MT_H
#define MT_H
#include "stdafx.h"
#include "afxinet.h"
typedef struct PARAM{
CListBox * pListBox;
CString strFtp;
CString strUCount;
CString strUPwd;
}*PPARAM;
UINT _cdecl mtQuery(LPVOID pParam)
{
PPARAM pparam = (PPARAM)pParam;
CListBox * pListBox = pparam->pListBox;
CString strFtp = pparam->strFtp;
CString strUCount = pparam->strUCount;
CString strUPwd = pparam->strUPwd;
CInternetSession * pSession = new CInternetSession(NULL, 1, PRE_CONFIG_INTERNET_ACCESS);
CFtpConnection * pFtpConnection;
try{
pFtpConnection = pSession->GetFtpConnection(strFtp, strUCount, strUPwd, 21, TRUE);
}
catch (CInternetException* pEx){
TCHAR error[1024] = { 0 };
pEx->GetErrorMessage(error, 1024);
AfxMessageBox(error);
pEx->Delete();
pFtpConnection = NULL;
}
if (pFtpConnection != NULL)
{
CFtpFileFind * pFileFind = new CFtpFileFind(pFtpConnection);
if (pFileFind != NULL)
{
BOOL bContinue = pFileFind->FindFile(L"*");
if (!bContinue)
{
pFileFind->Close();
pFileFind = NULL;
}
while (bContinue)
{
bContinue = pFileFind->FindNextFile();
CString strFileName = pFileFind->GetFileName();
if (pFileFind->IsDirectory())
{
strFileName = L"[" + strFileName + L"]";
}
pparam->pListBox->AddString(strFileName);
}
if (pFileFind != NULL)
{
pFileFind->Close();
pFileFind = NULL;
}
delete pFileFind;
}
if (pFtpConnection!=NULL)
{
pFtpConnection->Close();
delete pFtpConnection;
}
if (pSession!=NULL)
{
pSession->Close();
delete pSession;
}
}
return 0;
}
#endif
好的。問題已經找到了。。由於調用AfxBeginThread之後,主線程把這個線程的參數個delete掉了。。才開始多線程,,意識不夠到位啊~~~