Running multiple C# Task Async

前端 未结 3 1416
暗喜
暗喜 2021-01-04 19:48

Hi normally I would do this with a Background Worker, but I would like to do it with C# Task instead, just to understand Task better.

The thing is that I have a clas

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 20:39

    When you get the Result property, you are effectively waiting for the task to complete. It will execute on a background thread but you are waiting in your main thread for the completion before you start the next thread.

    See the MSDN doc for details.

    You should be able to just assign your properties from the background thread:

    Task.Factory.StartNew(() => Number1 = GenerateResult());
    

    WPF databinding will take care of marshalling the PropertyChanged event to the correct dispatcher thread.

提交回复
热议问题