0

I'm trying to use a WPF window as a message popup that will close once a task has been performed. All the documentation I've seen says that this can't be done with a messageBox, that's why I'm going with the WPF. I found one code snip that allowed me to open the WPF window but it wouldn't progress the application to the next process. Below is the last code example I found that I thought showed promise but the window isn't opening -

        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                string filePath = "my new directory";
                var popup = new PopupTest();

                popup.Dispatcher.BeginInvoke
                    (System.Windows.Threading.DispatcherPriority.Normal,
                    (Action)(() =>
                    {
                        popup.Show();
                        
                    }));

                // Do some console application stuff

                do
                {
                    Directory.CreateDirectory(filePath);
                } while (!Directory.Exists(filePath));

                popup.Close();            
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
    } 

The cs.xaml file is just the default

    /// Interaction logic for PopupTest.xaml
    /// </summary>
    public partial class PopupTest : Window
    {
        public PopupTest()
        {
            InitializeComponent();
        }
    }

I feel like this should be simpler than I'm making it. Anything that can point me in the right direction is appreciated.

KarkMump
  • 81
  • 1
  • 8
  • IMHO, just create a WPF application if you want to show some sort of window. Why are you mixing console and ui projects, why do you need both? [This may be worth](https://stackoverflow.com/questions/4509714/how-to-start-the-wpf-window-from-console-programmatically) checking out as well. – Trevor Dec 09 '21 at 18:14
  • This functionality, a warning popup until an action is completed, is being included into a larger existing application. What I included was just an approach that I found online. I checked out the link you included and `Application app = new Application (); app.Run(new Window1());` opens the window but doesn't allow the application to proceed. I tried adding that line to it's own thread ` var popup = new PopupTest(); Application app = new Application(); Thread thread1 = new Thread(()=> app.Run(popup)); thread1.Start(); ` But that doesn't open the window either – KarkMump Dec 09 '21 at 20:27

1 Answers1

0

You need to reference the WPF assemblies and create and run a System.Windows.Application on the STA thread:

[STAThread]
static void Main(string[] args)
{
    var app = new System.Windows.Application();
    app.Run(new PopupTest());
}

The Run method blocks and doesn't return until the app is shut down.

If you want to do some stuff while the app is running, you need to do this on another thread:

    [STAThread]
    static async Task Main(string[] args)
    {

        Task t = Task.Run(() => 
        {
            string filePath = "my new directory";
            do
            {
                Directory.CreateDirectory(filePath);
            } while (!Directory.Exists(filePath));
        });

        var app = new System.Windows.Application();
        app.Run(new MainWindow());

        await t;
    }
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Step in the right direction but I can't get the Window to close after the Task completes. Using the code above as a jumping off point I tried placing `app.MainWindow.Close();` after `app.Run` but it appears that line never gets hit. I tried instaniating the window before the Task and placing `app.MainWindow.Close()` within the Task, after the while loop, but that seemed to freeze up the application. – KarkMump Dec 10 '21 at 15:32
  • You don't await the task *until* the window and application has been closed. As I said, "the `Run` method *blocks* and *doesn't return* until the app is shut down." What exaclty are you trying to accomplish? When and how do you want to close the window? – mm8 Dec 15 '21 at 16:00