http listener with the background worker?

Oct 15, 2014 at 6:52 AM
What is the best practice about the following situation?

void handle_request(http_request request)
{
// wait background thread to notify the result

request.reply(received_result);
}

Can I avoid using .wait() in handle_request() method?
I guess the task_completion_event can help,
( http://msdn.microsoft.com/en-us/library/hh873170.aspx )
but I have no idea how to use it with the "http_request" object.
Coordinator
Oct 16, 2014 at 7:04 PM
Hi sunghun,

The http_request;:reply(...) function returns a task<http_response>. I'm not entirely use what you mean by 'wait background thread to notify result'. But what you can do is you can hook up a task continuation to notify when the response has been completely sent to the client. This could be done with something like the following:
void handle_request(http_request request)
{
    request.reply(/* create and insert response here */).then([]()
    {
        // response has been sent to the client
    });
}
Another option is you can save the task to wait on at a later point in time.
void handle_request(http_request request)
{
    pplx::task<http_response> responseTask = request.reply(/* create and insert response here */);
}
To learn more about programming with task parallelism I recommend you take a look here.

Steve
Marked as answer by sunghun on 10/24/2014 at 12:08 AM