单线程没问题,多线程出问题

chenzhichao2008 chenzhichao2008 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大家好,请各位高手帮忙看一下,谢谢!


如果程序调用Go(url,depth)
可以运行

如果改用多线程,调用Run(url,depth)
就会出错

工程设置:debug multiThreaded dll


bool CEngine::GetSourceHtml( const CString &theUrl, CStringBuf &buf )
{
CInternetFile* file = NULL;
CInternetSession session; //当执行这个一句时,theUrl值竟然被改变了
CString str;
try
{
// 试着连接到指定URL
file = (CInternetFile*) session.OpenURL(theUrl);
}
catch (CInternetException* m_pException)
{
m_pException->Delete();
return false;
}
buf.m_buf="";
while( file->ReadString(str) != NULL )
{
buf.m_buf+=str;
}
buf.m_language = HTML_LAN_CHINESE;
return true;
}

void CEngine::Run(const CString &url, int depth)
{
SetUrlAndDepth(url,depth);
CloseHandle( CreateThread(NULL, 0, Go2, (LPVOID)this, 0, NULL) );
}

DWORD WINAPI CEngine::Go2( LPVOID lpParam )
{
CEngine *g = (CEngine*) lpParam;
g->Go(g->m_url, g->m_depth);
AfxMessageBox(_T("complete"));
return 1;
}

bool CEngine::Go(const CString &url, int depth)
{
CComPtr<IDispatch> spDispatch;
MSHTML::IHTMLDocument2Ptr pDoc;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLAnchorElement> pLoct;
CStringBuf buf;
MSHTML::IHTMLElementCollectionPtr pCollection;
MSHTML::IHTMLElementPtr pElement;

if( depth <= 0 || !m_hash.Insert((LPCSTR)url) )
return false;

if( !GetSourceHtml(url, buf) )
return false;
pDoc = GetDocument(buf);
...
}
...全文

相关推荐

评论 18
收藏 已收藏
分享
返回顶部
chenzhichao2008
进程/线程/DLL 15,466 成员 49,169 内容