Quitting WPF Application after Window Closed
up vote
0
down vote
favorite
I have a method in class where I call loginWindow.ShowDialog();
which brings up a Window, however when you press Close (X in top right) it doesn't Quit the application, rather continues to run whatever is below loginWindow.ShowDialog();
in that method.
How am I able to quit the application entirely if that Window is closed?
I tried to use:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
System.Windows.Application.Current.Shutdown();
base.OnClosing(e);
}
Although this didn't shut the application down, which confuses me. When I was using loginWindow.Show();
this wasn't a problem.
c# wpf exit
add a comment |
up vote
0
down vote
favorite
I have a method in class where I call loginWindow.ShowDialog();
which brings up a Window, however when you press Close (X in top right) it doesn't Quit the application, rather continues to run whatever is below loginWindow.ShowDialog();
in that method.
How am I able to quit the application entirely if that Window is closed?
I tried to use:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
System.Windows.Application.Current.Shutdown();
base.OnClosing(e);
}
Although this didn't shut the application down, which confuses me. When I was using loginWindow.Show();
this wasn't a problem.
c# wpf exit
1
What is your shutdown mode?
– JohnB
Nov 21 at 2:14
I don't think I've defined it? The problem is for only this specific Window do I want to quit the application when Close is pressed. I have other Windows that when close is pressed, I just use Hide(); as I do not want to quit the application.
– Explorex
Nov 21 at 2:50
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a method in class where I call loginWindow.ShowDialog();
which brings up a Window, however when you press Close (X in top right) it doesn't Quit the application, rather continues to run whatever is below loginWindow.ShowDialog();
in that method.
How am I able to quit the application entirely if that Window is closed?
I tried to use:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
System.Windows.Application.Current.Shutdown();
base.OnClosing(e);
}
Although this didn't shut the application down, which confuses me. When I was using loginWindow.Show();
this wasn't a problem.
c# wpf exit
I have a method in class where I call loginWindow.ShowDialog();
which brings up a Window, however when you press Close (X in top right) it doesn't Quit the application, rather continues to run whatever is below loginWindow.ShowDialog();
in that method.
How am I able to quit the application entirely if that Window is closed?
I tried to use:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
System.Windows.Application.Current.Shutdown();
base.OnClosing(e);
}
Although this didn't shut the application down, which confuses me. When I was using loginWindow.Show();
this wasn't a problem.
c# wpf exit
c# wpf exit
edited Nov 21 at 1:53
asked Nov 21 at 1:47
Explorex
347
347
1
What is your shutdown mode?
– JohnB
Nov 21 at 2:14
I don't think I've defined it? The problem is for only this specific Window do I want to quit the application when Close is pressed. I have other Windows that when close is pressed, I just use Hide(); as I do not want to quit the application.
– Explorex
Nov 21 at 2:50
add a comment |
1
What is your shutdown mode?
– JohnB
Nov 21 at 2:14
I don't think I've defined it? The problem is for only this specific Window do I want to quit the application when Close is pressed. I have other Windows that when close is pressed, I just use Hide(); as I do not want to quit the application.
– Explorex
Nov 21 at 2:50
1
1
What is your shutdown mode?
– JohnB
Nov 21 at 2:14
What is your shutdown mode?
– JohnB
Nov 21 at 2:14
I don't think I've defined it? The problem is for only this specific Window do I want to quit the application when Close is pressed. I have other Windows that when close is pressed, I just use Hide(); as I do not want to quit the application.
– Explorex
Nov 21 at 2:50
I don't think I've defined it? The problem is for only this specific Window do I want to quit the application when Close is pressed. I have other Windows that when close is pressed, I just use Hide(); as I do not want to quit the application.
– Explorex
Nov 21 at 2:50
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I don't know how your application runs, based on your sample code I have two solutions.
Solution 1:- Every window has DialogResult
property. Inside the OnClosing
event assign DialogResult = true;
and call the Shutdown method. The windows who is responsible to call will get the result from return value of ShowDialog()
method
For example:-
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
DialogResult = true;
System.Windows.Application.Current.Shutdown();
}
Below event, is from the First screen, calling the Second Window.
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow secondWindow = new SecondWindow();
var dialogResult = secondWindow.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value == false)
{
// any code of yours which must not be executed after the second
// window has closed the process
}
}
Once, the DialogResult
is assigned true, the first window will check only if it is false execute the below code or else ignore.
Solution 2:- We will get the Current Application Running Process and Kill the whole process that is your whole application.
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
@Explorex were my solution relevant to you ? if not could you tell me why it isn't working for you.So that I can think more on this.
– Satish Pai
Nov 22 at 5:11
add a comment |
up vote
0
down vote
There is a shutdown mode that you can define in the App.Xaml
The default option is OnLastWindowClosed. If it is OnExplicitShutdown than the application wants you to call Application.Shutdown(). What this means is, if you close all windows the application is still running because it is expecting a Application.Shutdown() to be called. This is an explicit shutdown.
Other two options are implicit meaning the Application.Shutdown() method will be called when the last window is closed or when the main window is closed.
Can you check what option you have defined?
I didn't define it
– Explorex
Nov 21 at 13:55
I just tried all three, none of which work application continues to run after this window has been closed
– Explorex
Nov 21 at 14:01
In that case you probably have some code that is preventig the application to shutdown. If you create a new WPF project you can see how ShutdownMode property works. By default, when you close the last Window the application shuts down. I can think of nothing else that would prevent this behavior by default. Maybe a background worker or something...
– Rob
Nov 21 at 15:18
I'm sure it has something to do with me using window.showdialog() rather than window.show()
– Explorex
Nov 21 at 21:46
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I don't know how your application runs, based on your sample code I have two solutions.
Solution 1:- Every window has DialogResult
property. Inside the OnClosing
event assign DialogResult = true;
and call the Shutdown method. The windows who is responsible to call will get the result from return value of ShowDialog()
method
For example:-
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
DialogResult = true;
System.Windows.Application.Current.Shutdown();
}
Below event, is from the First screen, calling the Second Window.
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow secondWindow = new SecondWindow();
var dialogResult = secondWindow.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value == false)
{
// any code of yours which must not be executed after the second
// window has closed the process
}
}
Once, the DialogResult
is assigned true, the first window will check only if it is false execute the below code or else ignore.
Solution 2:- We will get the Current Application Running Process and Kill the whole process that is your whole application.
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
@Explorex were my solution relevant to you ? if not could you tell me why it isn't working for you.So that I can think more on this.
– Satish Pai
Nov 22 at 5:11
add a comment |
up vote
0
down vote
I don't know how your application runs, based on your sample code I have two solutions.
Solution 1:- Every window has DialogResult
property. Inside the OnClosing
event assign DialogResult = true;
and call the Shutdown method. The windows who is responsible to call will get the result from return value of ShowDialog()
method
For example:-
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
DialogResult = true;
System.Windows.Application.Current.Shutdown();
}
Below event, is from the First screen, calling the Second Window.
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow secondWindow = new SecondWindow();
var dialogResult = secondWindow.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value == false)
{
// any code of yours which must not be executed after the second
// window has closed the process
}
}
Once, the DialogResult
is assigned true, the first window will check only if it is false execute the below code or else ignore.
Solution 2:- We will get the Current Application Running Process and Kill the whole process that is your whole application.
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
@Explorex were my solution relevant to you ? if not could you tell me why it isn't working for you.So that I can think more on this.
– Satish Pai
Nov 22 at 5:11
add a comment |
up vote
0
down vote
up vote
0
down vote
I don't know how your application runs, based on your sample code I have two solutions.
Solution 1:- Every window has DialogResult
property. Inside the OnClosing
event assign DialogResult = true;
and call the Shutdown method. The windows who is responsible to call will get the result from return value of ShowDialog()
method
For example:-
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
DialogResult = true;
System.Windows.Application.Current.Shutdown();
}
Below event, is from the First screen, calling the Second Window.
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow secondWindow = new SecondWindow();
var dialogResult = secondWindow.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value == false)
{
// any code of yours which must not be executed after the second
// window has closed the process
}
}
Once, the DialogResult
is assigned true, the first window will check only if it is false execute the below code or else ignore.
Solution 2:- We will get the Current Application Running Process and Kill the whole process that is your whole application.
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
I don't know how your application runs, based on your sample code I have two solutions.
Solution 1:- Every window has DialogResult
property. Inside the OnClosing
event assign DialogResult = true;
and call the Shutdown method. The windows who is responsible to call will get the result from return value of ShowDialog()
method
For example:-
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
DialogResult = true;
System.Windows.Application.Current.Shutdown();
}
Below event, is from the First screen, calling the Second Window.
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow secondWindow = new SecondWindow();
var dialogResult = secondWindow.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value == false)
{
// any code of yours which must not be executed after the second
// window has closed the process
}
}
Once, the DialogResult
is assigned true, the first window will check only if it is false execute the below code or else ignore.
Solution 2:- We will get the Current Application Running Process and Kill the whole process that is your whole application.
private void SecondWindow_OnClosing(object sender, CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
answered Nov 21 at 4:55
Satish Pai
1659
1659
@Explorex were my solution relevant to you ? if not could you tell me why it isn't working for you.So that I can think more on this.
– Satish Pai
Nov 22 at 5:11
add a comment |
@Explorex were my solution relevant to you ? if not could you tell me why it isn't working for you.So that I can think more on this.
– Satish Pai
Nov 22 at 5:11
@Explorex were my solution relevant to you ? if not could you tell me why it isn't working for you.So that I can think more on this.
– Satish Pai
Nov 22 at 5:11
@Explorex were my solution relevant to you ? if not could you tell me why it isn't working for you.So that I can think more on this.
– Satish Pai
Nov 22 at 5:11
add a comment |
up vote
0
down vote
There is a shutdown mode that you can define in the App.Xaml
The default option is OnLastWindowClosed. If it is OnExplicitShutdown than the application wants you to call Application.Shutdown(). What this means is, if you close all windows the application is still running because it is expecting a Application.Shutdown() to be called. This is an explicit shutdown.
Other two options are implicit meaning the Application.Shutdown() method will be called when the last window is closed or when the main window is closed.
Can you check what option you have defined?
I didn't define it
– Explorex
Nov 21 at 13:55
I just tried all three, none of which work application continues to run after this window has been closed
– Explorex
Nov 21 at 14:01
In that case you probably have some code that is preventig the application to shutdown. If you create a new WPF project you can see how ShutdownMode property works. By default, when you close the last Window the application shuts down. I can think of nothing else that would prevent this behavior by default. Maybe a background worker or something...
– Rob
Nov 21 at 15:18
I'm sure it has something to do with me using window.showdialog() rather than window.show()
– Explorex
Nov 21 at 21:46
add a comment |
up vote
0
down vote
There is a shutdown mode that you can define in the App.Xaml
The default option is OnLastWindowClosed. If it is OnExplicitShutdown than the application wants you to call Application.Shutdown(). What this means is, if you close all windows the application is still running because it is expecting a Application.Shutdown() to be called. This is an explicit shutdown.
Other two options are implicit meaning the Application.Shutdown() method will be called when the last window is closed or when the main window is closed.
Can you check what option you have defined?
I didn't define it
– Explorex
Nov 21 at 13:55
I just tried all three, none of which work application continues to run after this window has been closed
– Explorex
Nov 21 at 14:01
In that case you probably have some code that is preventig the application to shutdown. If you create a new WPF project you can see how ShutdownMode property works. By default, when you close the last Window the application shuts down. I can think of nothing else that would prevent this behavior by default. Maybe a background worker or something...
– Rob
Nov 21 at 15:18
I'm sure it has something to do with me using window.showdialog() rather than window.show()
– Explorex
Nov 21 at 21:46
add a comment |
up vote
0
down vote
up vote
0
down vote
There is a shutdown mode that you can define in the App.Xaml
The default option is OnLastWindowClosed. If it is OnExplicitShutdown than the application wants you to call Application.Shutdown(). What this means is, if you close all windows the application is still running because it is expecting a Application.Shutdown() to be called. This is an explicit shutdown.
Other two options are implicit meaning the Application.Shutdown() method will be called when the last window is closed or when the main window is closed.
Can you check what option you have defined?
There is a shutdown mode that you can define in the App.Xaml
The default option is OnLastWindowClosed. If it is OnExplicitShutdown than the application wants you to call Application.Shutdown(). What this means is, if you close all windows the application is still running because it is expecting a Application.Shutdown() to be called. This is an explicit shutdown.
Other two options are implicit meaning the Application.Shutdown() method will be called when the last window is closed or when the main window is closed.
Can you check what option you have defined?
answered Nov 21 at 13:48
Rob
1,063920
1,063920
I didn't define it
– Explorex
Nov 21 at 13:55
I just tried all three, none of which work application continues to run after this window has been closed
– Explorex
Nov 21 at 14:01
In that case you probably have some code that is preventig the application to shutdown. If you create a new WPF project you can see how ShutdownMode property works. By default, when you close the last Window the application shuts down. I can think of nothing else that would prevent this behavior by default. Maybe a background worker or something...
– Rob
Nov 21 at 15:18
I'm sure it has something to do with me using window.showdialog() rather than window.show()
– Explorex
Nov 21 at 21:46
add a comment |
I didn't define it
– Explorex
Nov 21 at 13:55
I just tried all three, none of which work application continues to run after this window has been closed
– Explorex
Nov 21 at 14:01
In that case you probably have some code that is preventig the application to shutdown. If you create a new WPF project you can see how ShutdownMode property works. By default, when you close the last Window the application shuts down. I can think of nothing else that would prevent this behavior by default. Maybe a background worker or something...
– Rob
Nov 21 at 15:18
I'm sure it has something to do with me using window.showdialog() rather than window.show()
– Explorex
Nov 21 at 21:46
I didn't define it
– Explorex
Nov 21 at 13:55
I didn't define it
– Explorex
Nov 21 at 13:55
I just tried all three, none of which work application continues to run after this window has been closed
– Explorex
Nov 21 at 14:01
I just tried all three, none of which work application continues to run after this window has been closed
– Explorex
Nov 21 at 14:01
In that case you probably have some code that is preventig the application to shutdown. If you create a new WPF project you can see how ShutdownMode property works. By default, when you close the last Window the application shuts down. I can think of nothing else that would prevent this behavior by default. Maybe a background worker or something...
– Rob
Nov 21 at 15:18
In that case you probably have some code that is preventig the application to shutdown. If you create a new WPF project you can see how ShutdownMode property works. By default, when you close the last Window the application shuts down. I can think of nothing else that would prevent this behavior by default. Maybe a background worker or something...
– Rob
Nov 21 at 15:18
I'm sure it has something to do with me using window.showdialog() rather than window.show()
– Explorex
Nov 21 at 21:46
I'm sure it has something to do with me using window.showdialog() rather than window.show()
– Explorex
Nov 21 at 21:46
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404212%2fquitting-wpf-application-after-window-closed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What is your shutdown mode?
– JohnB
Nov 21 at 2:14
I don't think I've defined it? The problem is for only this specific Window do I want to quit the application when Close is pressed. I have other Windows that when close is pressed, I just use Hide(); as I do not want to quit the application.
– Explorex
Nov 21 at 2:50