internal WaitProgressDialogController(WaitProgressDialog dialog, Func<Task> closeCallBack)
        {
            WrappedDialog = dialog;
            CloseCallback = closeCallBack;

            IsOpen = dialog.IsVisible;

        }
        public static Task<WaitProgressDialogController> ShowWaitProgressAsync(this MetroWindow window, string title, string message = null)
        {
            window.Dispatcher.VerifyAccess();

            return HandleOverlayOnShow(null, window).ContinueWith(z =>
            {
                return ((Task<WaitProgressDialogController>)window.Dispatcher.Invoke(new Func<Task<WaitProgressDialogController>>(() =>
                {
                    WaitProgressDialog dialog = new WaitProgressDialog(window);
                    dialog.Message = message;
                    dialog.Title = title;

                    SizeChangedEventHandler sizeHandler = SetupAndOpenDialog(window, dialog);
                    dialog.SizeChangedHandler = sizeHandler;

                    return dialog.WaitForLoadAsync().ContinueWith(x =>
                    {
                        if (DialogOpened != null)
                        {
                            window.Dispatcher.BeginInvoke(new Action(() => DialogOpened(window, new DialogStateChangedEventArgs()
                            {
                            })));
                        }

                        return new WaitProgressDialogController(dialog, () =>
                        {
                            dialog.OnClose();

                            if (DialogClosed != null)
                            {
                                window.Dispatcher.BeginInvoke(new Action(() => DialogClosed(window, new DialogStateChangedEventArgs()
                                {
                                })));
                            }

                            Task closingTask = (Task)window.Dispatcher.Invoke(new Func<Task>(() => dialog._WaitForCloseAsync()));
                            return closingTask.ContinueWith<Task>(a =>
                            {
                                return (Task)window.Dispatcher.Invoke(new Func<Task>(() =>
                                {
                                    window.SizeChanged -= sizeHandler;
                                    window.metroDialogContainer.Children.Remove(dialog); 
                                    return HandleOverlayOnHide(null, window);
                                }));
                            }).Unwrap();
                        });
                    });
                })));
            }).Unwrap();
        }