Example #1
0
        /// <summary>
        /// Returns a task that can be blocked against until a specific property changes.
        /// The property or properties should be marked with <see cref="BlockingPropertyAttribute"/> otherwise a completed task will be returned immediately.
        /// </summary>
        /// <param name="self">Class instance that contains the properties</param>
        /// <returns>Task that can be awaited until the decorated properties change</returns>
        public static Task <bool> BlockUntil(this INotifyPropertyChanged self)
        {
            TaskCompletionSource <bool> completionSource;

            if (self.TaskCompleted(out completionSource))
            {
                // The task is already finished
                return(Task.FromResult(true));
            }

            completionSource = new TaskCompletionSource <bool>();

            if (!CompletionSources.ContainsKey(self))
            {
                CompletionSources.Add(self, completionSource);
            }

            self.PropertyChanged += SelfOnPropertyChanged;
            return(completionSource.Task);
        }