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?
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.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 usinge.Cancel = true;
, then just callHide
on the window to "close" it andShow
to "open" it again later.I've not had chance to test this myself, but after reading up on your error, I found some information. Basically, it sounds like the
AppDomain
you are running in can only be used once per application - so perhaps you need to create a newAppDomain
each time you want to re-create the application. See here for some more information on this.Alternately, you could use
System.Diagnostics.Process.Start(...)
in order to fire off the application properly. Refer here for the documentation for theProcess
class.Finally, if you are just wanting to allow the user to run the application from the command line, it might be simpler to write a command script and execute that from the command line.
When you add window as a parameter to app.Run you link the lifetime of your app to your window. Don't do that: