是否可以在线程内终止SynchronousRequest?我不能使用SynchronousRequestAsync。
答案 0 :(得分:0)
是的。您可以通过设置Http对象的AbortCurrent属性来从单独的线程中止。您可以从后台线程调用SynchronousRequest,并从主线程设置AbortCurrent,或者相反。
例如,在C ++中:
static DWORD WINAPI myAbortThreadProc(LPVOID lpvThreadParm)
{
CkHttp *pHttp = (CkHttp *) lpvThreadParm;
Sleep(2000);
pHttp->put_AbortCurrent(true);
return 0;
}
bool HttpTesting::qa_abortCurrentSynchronousRequest(void)
{
const char *testName = "qa_abortCurrentSynchronousRequest";
CkHttp http;
// http://www.w3.org/TR/xhtml1/DTD/xhtml---.dtd
CkHttpRequest req;
req.put_HttpVerb("GET");
req.put_Path("/TR/xhtml1/DTD/xhtml---.dtd");
// Start a thread to do the abort...
DWORD threadID;
HANDLE hThread = CreateThread(0,0,myAbortThreadProc,(void *)&http,0,&threadID);
if (hThread == NULL)
{
printf("Failed to start thread!\n");
return false;
}
else
{
CloseHandle(hThread);
}
CkHttpResponse *resp = http.SynchronousRequest("www.w3.org",80,false,req);
if (!resp) return failed(testName,http);
printf("%s\n",http.lastErrorText());
delete resp;
return succeeded(testName);
}