How to continue a task on the main (UI) thread on windows 7?

Apr 29, 2014 at 10:37 PM
I'm having the same issue as this http://casablanca.codeplex.com/workitem/1.

Basically I'm downloading an image and when it is done downloading I need to call a callback on the UI thread so the image can be displayed. I haven't found anything related to this issue other than that link, and it doesn't really help.

It seems like this is quite simple to do for a windows store app, just use task_continuation_context::use_current(), but I can't find a solution for win7.

Thanks,
Tyler
Apr 30, 2014 at 6:50 AM
I think there isn't a simple way to do that.

You can create a window of type HWND_MESSAGE in ui thread and PostMessage to that window.
Coordinator
Apr 30, 2014 at 5:29 PM
Yes we don't have a feature like that in the C++ Rest SDK. You can only do that for Windows store applications. Tyler like mentioned in the closed issue you mention, you can supply your own scheduler which will do the execution on a particular thread.

Steve
Apr 30, 2014 at 8:22 PM
Are there any examples that show how to use your own scheduler? I looked at pplx_op_test.cpp and pplx_scheduler.cpp but I can't figure out how to do it (or even what's relevant in those files.)
Coordinator
May 1, 2014 at 3:39 AM
Hi Tyler,

We don't have a complete sample illustrating such, but if you look at the right parts you can see it done in the test cases.

Start by looking in pplx_scheduler.cpp, specifically at the class pplx_dflt_scheduler and notice how it implements the pplx::scheduler_interface. You can see here this is a simple scheduler based on top of the Windows thread pool. It implements the void schedule(pplx::TaskProc proc, void *param) function. In there is where if you were creating your own custom scheduler for updating a UI that you would schedule the work to your UI thread. Notice how in this example the Windows threadpool callback, DefaultWorkCallbackTest, executes the task by invoking the TaskProc with scheduler->m_proc(schedulerParam->m_param). You can then an instance of this scheduler is exposed through the function pplx::scheduler_interface & get_pplx_dflt_scheduler().

Now once you have a scheduler to use it you can specify it anywhere you create a task or attach a continuation to a task, i.e. pplx::create_task(...) and pplx::task::then(...). The second parameter is pplx::task_options and its constructor allows you to specify a scheduler_interface to execute the task on. Take a look in pplx_op_test.cpp, specifically the test called schedule_two_tasks and you can see pplx::create_task is called providing the scheduler created in pplx_scheduler.cpp.

Hope that helps,
Steve