Is it safe to store the TaskScheduler of the UI Thread in a field as following:
class TaskSchedulerReference {
private readonly _uiTaskScheduler;
// Constructor is called by the UI Thead
public TaskSchedulerReference() {
_uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext()
}
public TaskScheduler UiTaskScheduler { get { return _uiTaskScheduler; } }
}
...so that it is possible to notify the UI at any time from any background thread in a Task.ContinueWith(action, TaskSchedulerReference.UiTaskScheduler)
callback.
Is it guaranteed that the reference remains valid during the whole application life-cycle?