How to get notification that a System.Threading.Tasks.Task has completed

前端 未结 6 599
广开言路
广开言路 2020-12-14 15:14

I am currently replacing some home baked task functionality with a new implementation using the new System.Threading.Tasks functionality found in .net 4.

I have a sl

6条回答
  •  星月不相逢
    2020-12-14 15:48

    You can use the ContinueWith function with your routine as a first argument, and a task scheduler as the second argument given by TaskScheduler.FromCurrentSynchronizationContext().

    It goes like this:

    var task1 = new Task(() => {do_something_in_a_remote_thread();} );
    
    task1.ContinueWith(() =>  {do_something_in_the_ui_thread();},
    TaskScheduler.FromCurrentSynchronizationContext());
    

提交回复
热议问题