首页 > 解决方案 > 使用 ctrl+c 取消时抛出异常

问题描述

pplx::task<void> RestInterface::Getsxxxx()

{
utility::stringstream_t ss;
pplx::cancellation_token_source cts;
pplx::cancellation_token ct = cts.get_token();

ss<<"http://"<<URL<<":"<<PORT<<PATH<<ZALL;

http_client client(U(ss.str()));

///here were using the jSON function for URI to request the REST API
///else the name of the API can be passed to stream also.
http_request requestJson(methods::GET);

requestJson.headers().set_content_type(U("application/json"));

    ///you must perform the request
    ///against a server that provides JSON data.

    return client.request(requestJson, ct).then([=](http_response response) -> pplx::task<web::json::value>
    {
        ///If the status is OK extract the body of the response into a JSON value
        if(!getCancelledSate())
        {
            if (response.status_code() == status_codes::OK)
            {
                return response.extract_json();
            }
            else
            {
                // return an empty JSON value
                isConnected = false;
                return pplx::task_from_result(web::json::value());
            }
        }
        else
        {
            //try{
                cts.cancel();
            //}
            //catch(http_exception const & e)
            //{
            //    std::cout<<__FILE__<<"["<<__LINE__<<"]"<<"Exception:"<< e.what() << std::endl;
            //}
        }

    }, ct).then([=](pplx::task<web::json::value> previousTask)
    {
        try{
            std::cout <<" before previousTask.wait()"<< std::endl;
            ///Get the JSON value from the task and call the traversal method

            if(previousTask.wait() == pplx::task_status::canceled){
                //Do nothing
                std::cout <<" after previousTask.wait()"<< std::endl;
            }else{
                web::json::value const & dataValue = previousTask.get();
                m_dataJson = dataValue;

                ///clear string streambuffer
                m_Buffer.str("");
                m_Buffer.clear();
                m_Buffer << m_dataJson.serialize();

                utility::string_t streamJson = m_Buffer.str();

                if((strxxxxcmp(streamJson, APP_ACTIVE) < 0)){
                    isConnected = false;
                    //......So work
                }else{
                    //......So work
                }
            }
        }
        catch(http_exception const & e)
        {
            isConnected = false;
            std::cout<<__FILE__<<"["<<__LINE__<<"]"<<"Exception:"<< e.what() << std::endl;
        }
        });
}

void RestInterface::xxxxz()
{
    try
    {
        isConnected = true;
        Getsxxxx().wait();
    }
    catch(const std::exception &e)
    {
        std::cout<<__FILE__<<"["<<__LINE__<<"]"<<"Exception:"<< e.what() << std::endl;
        //printf("Error exception:%s\n", e.what());
    }
}

现在我有一个问题。我们有一个场景,我们必须在生成信号时取消任务,例如生成 SIGINT 时......当用户使用 Ctrl+C 给出 SIGINT 时有时我可以看到以下消息。

**/build/casablanca-FHxOgj/casablanca-2.8.0/Release/include/pplx/pplxtasks.h:1835: pplx::task_status pplx::details::_Task_impl_base::_Wait(): Assertion `_IsCompleted()' failed.
Aborted**

我认为某些任务引发了未处理的异常,但我不完全确定是哪一个以及原因。任何人都可以分享一些想法。

任何帮助都会很棒

标签: casablancacpprest-sdk

解决方案


推荐阅读