Можно ли завершить SynchronousRequest в потоке? - PullRequest
0 голосов
/ 08 октября 2018

Возможно ли завершить SynchronousRequest в потоке?Я не могу использовать SynchronousRequestAsync.

1 Ответ

0 голосов
/ 10 октября 2018

Да.Вы можете прервать работу из отдельного потока, установив свойство AbortCurrent объекта Http.Вы можете вызвать 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);
}
...