如何定制一款12306抢票浏览器――启动“人”线程(二)
有直接将IStream转换成m_spWindow,因为在转之前要将m_spWindow置为NULL。而恰恰是这个置为NULL的过程,可能会和之前SetWebBrowser的过程发生死锁。所以此处我用一个临时变量去接收转换结果,最后再将m_spWindow设置为该结果。
线程函数的代码是
[cpp]
VOID CAutoMan::ThreadFun()
{
m_dwQueryTime = QUERYTIMESLOW;
while ( WAIT_TIMEOUT == WaitForSingleObject(m_hStopEvent, m_dwQueryTime )) {
ConvertInterface();
if ( NULL == m_spWindow ) {
continue;
}
CComBSTR bstrUrl;
HRESULT hr = m_spWindow->get_LocationURL(&bstrUrl);
CComPtr spDoc;
CComPtr spDispatch;
hr = m_spWindow->get_Document(&spDispatch);
if ( FAILED(hr) || NULL == spDispatch ){
continue;
}
hr = spDispatch->QueryInterface( IID_IHTMLDocument2, (LPVOID*)&spDoc);
try {
if ( m_DealWebPage.IsQueryPage(spDoc, bstrUrl)) {
hr = m_DealWebPage.InsertButtonInQueryPage(spDoc);
if ( m_bStartQuery ) {
hr = m_DealWebPage.QueryTicketsInfo(spDoc);
if (FAILED(hr)) {
hr = m_DealWebPage.StartQueryInQueryPage(spDoc);
}
else {
}
}
else if ( m_DealWebPage.IsBookingPage(spDoc, bstrUrl) ) {
hr = m_DealWebPage.BookTickets(spDoc);
if (SUCCEEDED(hr)) {
// 待处理,退出线程
}
else {
}
}
}
catch(...) {
}
}
}
“人”线程会每隔m_dwQueryTime毫秒进行一次轮询操作。操作的内容就是:
1 查询当前UR L www.2cto.com
2 如果当前URL是订票查询页面(m_DealWebPage.IsQueryPage(spDoc, bstrUrl)),则进行
A 插入控制按钮(hr = m_DealWebPage.InsertButtonInQueryPage(spDoc);)
B 检查是否有票(hr = m_DealWebPage.QueryTicketsInfo(spDoc);)
C 在不存在其票的情况下点击刷新按钮的操作(hr = m_DealWebPage.StartQueryInQueryPage(spDoc);)
3 如果当前是确认订单页面(m_DealWebPage.IsBookingPage(spDoc, bstrUrl)),则进行订票操作(hr = m_DealWebPage.BookTickets(spDoc);)
由于为时已晚,我无法图文并茂的讲解之后的订票过程。今天就到此休息了,今天晚上我会结合12306页面讲解其上详细的过程。