none
Get back to the UI thread

    Dotaz

  • Hey,

    tpl is fantastic! Together with the underrated net.p2p wcf channel, i was able to build a highly scaling mesh, thanks to TPL. All the internal scheduling, load balancing (thx to non greedy blocks/bounding), was done by chaining up blocks. 

    Now i am a bit coonfused, i started to learn WPF :-) and was not able to get back status informations to the UI. Using Tasks i can do ContinueWith and use FromSync..Context. How can i achieve status updates with TPL:

    - Something was put in the BufferBlock

    - Something was retrieved from the BufferBlock

    -Something was handed over to an Action/TrensformBlock

    - An operation was completed within a *Block

    I cant manage to get back to the UI Thread, i am as well confused with Completion.ContinueWith

     

    Thanks

     Andreas

    5. února 2012 20:39

Odpovědi

  • Hi Relam.pago-

    That's right, by default the ActionBlock is executing on the ThreadPool.  If you want the delegate provided to the ActionBlock to run on the UI thread, you can pass an ExecutionDataflowBlockOptions to the ActionBlock constructor, and set that options.TaskScheduler to be a TaskScheduler for the UI thread (e.g. a scheduler returned from TaskScheduler.FromCurrentSynchronizationContext while on the UI thread).  Or, if you want to know when all of the ActionBlock's work has completed, you can use a continuation off of the ActionBlock to run on the UI thread, either by using ContinueWith passing in the right scheduler, or by using await (which by default will go back to the current SynchronizationContext or TaskScheduler).

    6. února 2012 15:53
    Vlastník

Všechny reakce

  • > I cant manage to get back to the UI Thread, i am as well confused with Completion.ContinueWith 

     

    Task.Factory.StartNew(() =>  // worker thread
    {
        return "data";     
    })
    .ContinueWith(task =>  // ui thread
    { 
        var data = task.Result;     
        // ...
    }, 
    TaskScheduler.FromCurrentSynchronizationContext());
    



    if you need to process data in a worker thread and send results to the UI-thread, then
    take a look at the example here.
      
        


    • Upravený Malobukv 5. února 2012 21:18
    5. února 2012 20:43
  • Tanks for your response,

    but as stated above, i am not confused with tasks, but with action/transfoprmblocks.

    example :-D:

    BufferBlock<int> bb = new BufferBlock<int>();
                ActionBlock<int> a1 = new ActionBlock<int>((a) =>
                                                            {
                                                                Thread.Sleep(100);
                                                                Console.WriteLine("Action A1 executing with value {0}", a);
                                                            }
                                                          );
    
     bb.LinkTo(a1);
    
    bb.Post(1);
    bb.Post(2);
    bb.Post(3);
    
    


    Lets, take this code, and call it from a ViewModel (WPF), i want to inform the ViewModel in the UI thread. E.g When something gots posted, pulled from the bufferblock and when the actionblock is finished. I think the Actionblock is not executing in the UI thread right?

     

    I could pass an Tuple<int, Action>, where the action is a method in the ui thread...

    6. února 2012 8:26
  • Hi Relam.pago-

    That's right, by default the ActionBlock is executing on the ThreadPool.  If you want the delegate provided to the ActionBlock to run on the UI thread, you can pass an ExecutionDataflowBlockOptions to the ActionBlock constructor, and set that options.TaskScheduler to be a TaskScheduler for the UI thread (e.g. a scheduler returned from TaskScheduler.FromCurrentSynchronizationContext while on the UI thread).  Or, if you want to know when all of the ActionBlock's work has completed, you can use a continuation off of the ActionBlock to run on the UI thread, either by using ContinueWith passing in the right scheduler, or by using await (which by default will go back to the current SynchronizationContext or TaskScheduler).

    6. února 2012 15:53
    Vlastník