Application terminates when window gets closed
-
Hi! I have something like this:
public class MyApp: System.Windows.Application
{
public MyApp()
{m\_Window = new Window(); //loads of other stuff
}
Window m_Window;
}and it gets instantiated from:
class Program
{
[STAThread]
static void Main()
{
MyApp app = new MyApp();
app.Run();
}
}Every time someone says
m_Window.Close()
the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M. -
Hi! I have something like this:
public class MyApp: System.Windows.Application
{
public MyApp()
{m\_Window = new Window(); //loads of other stuff
}
Window m_Window;
}and it gets instantiated from:
class Program
{
[STAThread]
static void Main()
{
MyApp app = new MyApp();
app.Run();
}
}Every time someone says
m_Window.Close()
the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M.It Works when I derive MyApp from System.Windows.Window but it looks quite ugly:
public class MyApp : Window
{
public MyApp()
{
this.Visibility = Visibility.Hidden; m\_Window = new Window();
}
Window m_Window;
}
class Program
{
[STAThread]
static void Main()
{
Application app = new Application(); MyApp win = new MyApp(); app.Run();
}
}
-
It Works when I derive MyApp from System.Windows.Window but it looks quite ugly:
public class MyApp : Window
{
public MyApp()
{
this.Visibility = Visibility.Hidden; m\_Window = new Window();
}
Window m_Window;
}
class Program
{
[STAThread]
static void Main()
{
Application app = new Application(); MyApp win = new MyApp(); app.Run();
}
}
m_Window.Close() is different than Visibility.Hidden now isn't it. In the first case, yes, the application will terminate because the form has been closed, thus terminating the message loop. In the second case the window is not closed, just hidden, so the message loop continues and the application does not terminate.
only two letters away from being an asset
-
m_Window.Close() is different than Visibility.Hidden now isn't it. In the first case, yes, the application will terminate because the form has been closed, thus terminating the message loop. In the second case the window is not closed, just hidden, so the message loop continues and the application does not terminate.
only two letters away from being an asset
So what's your point? In the first case I derived it from 'Application' and in the second I derived it from 'Window'. But I don't want to derive it from 'Window' because it is no window.
-
Hi! I have something like this:
public class MyApp: System.Windows.Application
{
public MyApp()
{m\_Window = new Window(); //loads of other stuff
}
Window m_Window;
}and it gets instantiated from:
class Program
{
[STAThread]
static void Main()
{
MyApp app = new MyApp();
app.Run();
}
}Every time someone says
m_Window.Close()
the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M.Hi, a normal window app has an Application.Run(new Form1()); in its static main method. The Run method will show the Form1 instance (and run a message loop to do that), and it will return when the form gets closed or Application.Exit() gets called. as an alternative you can call Application.Run(); without arguments; that one will start a message pump and return when Application.Exit() gets called. Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hi, a normal window app has an Application.Run(new Form1()); in its static main method. The Run method will show the Form1 instance (and run a message loop to do that), and it will return when the form gets closed or Application.Exit() gets called. as an alternative you can call Application.Run(); without arguments; that one will start a message pump and return when Application.Exit() gets called. Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
"as an alternative you can call Application.Run(); without arguments;" yeah thanks, but I think taht is what I showed already in my first posting. Maybe I should try it verbally: 1.) I want an application that instantiates a class 2.) This class should open a window 3.) When the window gets closed the class should stay 'alive' (and therefore also the application must not get terminated) Mario M.
-
Hi! I have something like this:
public class MyApp: System.Windows.Application
{
public MyApp()
{m\_Window = new Window(); //loads of other stuff
}
Window m_Window;
}and it gets instantiated from:
class Program
{
[STAThread]
static void Main()
{
MyApp app = new MyApp();
app.Run();
}
}Every time someone says
m_Window.Close()
the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M.Forget it, I'll take the "derive from Window" approach Thanks, Mario M.
-
"as an alternative you can call Application.Run(); without arguments;" yeah thanks, but I think taht is what I showed already in my first posting. Maybe I should try it verbally: 1.) I want an application that instantiates a class 2.) This class should open a window 3.) When the window gets closed the class should stay 'alive' (and therefore also the application must not get terminated) Mario M.
Hi, one way of doing that is: - create a Windows app with a main form - hide the main form (it initially might have served as a splash screen) - create another window/form/whatever, maybe inside the main form's Load event - do whatever you want with that window/form/whatever, closing it does not affect the (invisible) mainform, hence the app continues (doing what I might ask). :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Forget it, I'll take the "derive from Window" approach Thanks, Mario M.
-
So what's your point? In the first case I derived it from 'Application' and in the second I derived it from 'Window'. But I don't want to derive it from 'Window' because it is no window.
The point is, learn the difference between window.close and window.hidden
only two letters away from being an asset
-
Hi! "Which, is what Luc pointed out, very well explaint, here!" And what I already posted here Ciao Mario M.
-
The point is, learn the difference between window.close and window.hidden
only two letters away from being an asset
How about looking at the two DIFFERENT code samples I posted? In the first snipped I create an App that has a window as member (only one window). When the window gets closed also the app terminates. And in the second sample I create a hidden window that has an additional window as member (two windows). I don't say: m_window.hidden! I hide the parent! But I think creating a hidden window just to make sure that the class itself survives when one of its members gets closed is quite ugly. So, please have a look at the code first, before you start getting wise! ;-) Mario M.
-
Hi! I have something like this:
public class MyApp: System.Windows.Application
{
public MyApp()
{m\_Window = new Window(); //loads of other stuff
}
Window m_Window;
}and it gets instantiated from:
class Program
{
[STAThread]
static void Main()
{
MyApp app = new MyApp();
app.Run();
}
}Every time someone says
m_Window.Close()
the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M.Problem solved:
[STAThread]
static void Main()
{
Application app = new Application();
app.ShutdownMode = ShutdownMode.OnExplicitShutdown; //that's the trick
MyApp win = new MyApp();
app.Run();
}class MyApp { public MyApp() { m\_Win = new Window(); m\_Win.ShowDialog(); } Window m\_Win; }
}
-
Hi! "Which, is what Luc pointed out, very well explaint, here!" And what I already posted here Ciao Mario M.
-
How about looking at the two DIFFERENT code samples I posted? In the first snipped I create an App that has a window as member (only one window). When the window gets closed also the app terminates. And in the second sample I create a hidden window that has an additional window as member (two windows). I don't say: m_window.hidden! I hide the parent! But I think creating a hidden window just to make sure that the class itself survives when one of its members gets closed is quite ugly. So, please have a look at the code first, before you start getting wise! ;-) Mario M.
MarioMARTIN wrote:
before you start getting wise!
Too late, already am.
only two letters away from being an asset