ookii-dialogs / ookii-dialogs-wpf

Awesome dialogs for Windows Desktop applications built with Microsoft .NET (WPF)
BSD 3-Clause "New" or "Revised" License
1.14k stars 85 forks source link

[HELP] How to support async fucntions with ProgressDialog. #42

Closed nlogozzo closed 3 years ago

nlogozzo commented 3 years ago

Hi I have a ProgressDialogService meant to take an async function and run it in the ProgressDialog as some async functions take some time.

using Ookii.Dialogs.Wpf;
using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace Nickvision.WPF.MVVM.Services
{
    /// <summary>
    /// A service that contains methods for working with Progress Dialogs
    /// </summary>
    public class ProgressDialogService : IProgressDialogService
    {
        private Func<Task> _toExecute;

        /// <summary>
        /// Shows a progress dialog
        /// </summary>
        /// <param name="description">The description of the task</param>
        /// <param name="task">The async task</param>
        public void ShowDialog(string description, Func<Task> task)
        {
            using var progressDialog = new ProgressDialog()
            {
                WindowTitle = "Progress Dialog",
                Text = description,
                Description = "Please Wait...",
                ShowTimeRemaining = true,
                ShowCancelButton = false
            };
            _toExecute = task;
            progressDialog.DoWork += DoWork;
            progressDialog.ShowDialog();
        }

        /// <summary>
        /// Executes a command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">DoWorkEventArgs</param>
        public async void DoWork(object sender, DoWorkEventArgs e) => await DoWorkAsync(sender, e);

        /// <summary>
        /// Executes a command asynchronously
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">DoWorkEventArgs</param>
        /// <returns></returns>
        public async Task DoWorkAsync(object sender, DoWorkEventArgs e) => await _toExecute();
    }
}

However, when I try to show the dialog I get no error and no dialog shows up:

private void Button_Click(object sender, RoutedEventArgs e)
        {
            var prog = new ProgressDialogService();
            prog.ShowDialog("Testing...", async () => await Task.Delay(10000));
        }

Any ideas?