Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6773065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:37:21+00:00 2026-05-26T15:37:21+00:00

I want to open a WPF Window from a Console application. After referring to

  • 0

I want to open a WPF Window from a Console application. After referring to this post, it works fine.

The problem is: When the user closed the WPF Window (manually), it can no long be re-opened from the Console, throwing the exception message: “Cannot create more than one System.Windows.Application instance in the same AppDomain.”

Here is the code:

class Program
    {
        static void Main(string[] args)
        {
            string input=null;
            while ((input = Console.ReadLine()) == "y")
            {
                //Works fine at the first iteration,
                //But failed at the second iteration.
                StartWpfThread();
            }
        }
        private static void OpenWindow()
        {
            //Exception(Cannot create more than one System.Windows.Application instance in the same AppDomain.)
            //is thrown at the second iteration.
            var app = new System.Windows.Application();
            var window = new System.Windows.Window();
            app.Run(window);
            //User  closes the opened window manually.
        }
        private static void StartWpfThread()
        {
            var thread = new Thread(() =>
            {
                OpenWindow();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = false;
            thread.Start();
        }
    }

How can I re-open the WPF Window?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:37:22+00:00Added an answer on May 26, 2026 at 3:37 pm

    You should not create the application together with the window but only once separately, also make sure that it does not exit after the window is closed by setting the ShutdownMode respectively, e.g.

    class Program
    {
        static Application app;
        static void Main(string[] args)
        {
            var appthread = new Thread(new ThreadStart(() =>
                {
                    app = new Application();
                    app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    app.Run();
                }));
            appthread.SetApartmentState(ApartmentState.STA);
            appthread.Start();
    
            while (true)
            {
                var key =Console.ReadKey().Key;
                // Press 1 to create a window
                if (key == ConsoleKey.D1)
                {
                    // Use of dispatcher necessary as this is a cross-thread operation
                    DispatchToApp(() => new Window().Show());
                }
                // Press 2 to exit
                if (key == ConsoleKey.D2)
                {
                    DispatchToApp(() => app.Shutdown());
                    break;
                }
            }
        }
    
        static void DispatchToApp(Action action)
        {
            app.Dispatcher.Invoke(action);
        }
    }
    

    Also if you want to re-open the very same window make sure it is never closed completely, to do that you can handle the Closing event and cancel it using e.Cancel = true;, then just call Hide on the window to “close” it and Show to “open” it again later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to open a folder window, in the appropriate file manager, from within
I want to open window2.xaml from the window1.xaml window as an emergent (floating) window.
In my application (WPF) i have this window: public partial class Window1 : Window
I am new to C# and WPF. I want to open a new window
i'm developing an application (wpf) that have 3 windows. in the main window user
In a WPF application, when a user clicks on a button I want to
I want open a window and then does something after it finishes loading, is
I want open a path to vim from Screen's copy-mode by Ctrl-A f similarly
I want to open a save file dialog, have the user enter a filename,
I want to open a file dialog box in user control. I used using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.