Beispiel #1
0
        public AsyncTaskWatcher(Task <TResult> task, PostCompletionMethod postMethod)
        {
            Task = task;

            if (!task.IsCompleted)
            {
                var x = WatchTaskAsync(task, postMethod);
            }
        }
Beispiel #2
0
        public async Task WatchTaskAsync(Task task, PostCompletionMethod postMethod)
        {
            try
            {
                await task;
            }
            catch
            {
                // I want to capture any exceptions and set properties so that the error handling is done further up the stack
            }

            if (postMethod != null && task.IsCompleted)
            {
                log.Debug("Post completion method started");
                postMethod();
                log.Debug("Post completion method finished");
            }

            var propertyChanged = PropertyChanged;

            if (propertyChanged == null)
            {
                return;
            }

            propertyChanged(this, new PropertyChangedEventArgs("Status"));
            propertyChanged(this, new PropertyChangedEventArgs("IsBusy"));
            propertyChanged(this, new PropertyChangedEventArgs("IsNotCompleted"));

            if (task.IsCanceled)
            {
                log.Debug("Task cancelled");
                propertyChanged(this, new PropertyChangedEventArgs("IsCanceled"));
            }
            else if (task.IsFaulted)
            {
                log.Debug("Task faulted");
                propertyChanged(this, new PropertyChangedEventArgs("IsFaulted"));
                propertyChanged(this, new PropertyChangedEventArgs("Exception"));
                propertyChanged(this, new PropertyChangedEventArgs("InnerException"));
                propertyChanged(this, new PropertyChangedEventArgs("ErrorMessage"));
            }
            else
            {
                log.Debug("Task completed successfully");
                propertyChanged(this, new PropertyChangedEventArgs("IsSuccessfullyCompleted"));
                propertyChanged(this, new PropertyChangedEventArgs("Result"));
            }
        }