Quitting WPF Application after Window Closed












0















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.










share|improve this question




















  • 1





    What is your shutdown mode?

    – JohnB
    Nov 21 '18 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 '18 at 2:50
















0















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.










share|improve this question




















  • 1





    What is your shutdown mode?

    – JohnB
    Nov 21 '18 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 '18 at 2:50














0












0








0


1






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 1:53







Explorex

















asked Nov 21 '18 at 1:47









ExplorexExplorex

617




617








  • 1





    What is your shutdown mode?

    – JohnB
    Nov 21 '18 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 '18 at 2:50














  • 1





    What is your shutdown mode?

    – JohnB
    Nov 21 '18 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 '18 at 2:50








1




1





What is your shutdown mode?

– JohnB
Nov 21 '18 at 2:14





What is your shutdown mode?

– JohnB
Nov 21 '18 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 '18 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 '18 at 2:50












2 Answers
2






active

oldest

votes


















0














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();
}





share|improve this answer
























  • @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 '18 at 5:11





















0














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?






share|improve this answer
























  • I didn't define it

    – Explorex
    Nov 21 '18 at 13:55






  • 1





    I just tried all three, none of which work application continues to run after this window has been closed

    – Explorex
    Nov 21 '18 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 '18 at 15:18











  • I'm sure it has something to do with me using window.showdialog() rather than window.show()

    – Explorex
    Nov 21 '18 at 21:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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();
}





share|improve this answer
























  • @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 '18 at 5:11


















0














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();
}





share|improve this answer
























  • @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 '18 at 5:11
















0












0








0







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();
}





share|improve this answer













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();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 4:55









Satish PaiSatish Pai

8261610




8261610













  • @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 '18 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 '18 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 '18 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 '18 at 5:11















0














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?






share|improve this answer
























  • I didn't define it

    – Explorex
    Nov 21 '18 at 13:55






  • 1





    I just tried all three, none of which work application continues to run after this window has been closed

    – Explorex
    Nov 21 '18 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 '18 at 15:18











  • I'm sure it has something to do with me using window.showdialog() rather than window.show()

    – Explorex
    Nov 21 '18 at 21:46
















0














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?






share|improve this answer
























  • I didn't define it

    – Explorex
    Nov 21 '18 at 13:55






  • 1





    I just tried all three, none of which work application continues to run after this window has been closed

    – Explorex
    Nov 21 '18 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 '18 at 15:18











  • I'm sure it has something to do with me using window.showdialog() rather than window.show()

    – Explorex
    Nov 21 '18 at 21:46














0












0








0







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?






share|improve this answer













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?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 13:48









RobRob

1,0421123




1,0421123













  • I didn't define it

    – Explorex
    Nov 21 '18 at 13:55






  • 1





    I just tried all three, none of which work application continues to run after this window has been closed

    – Explorex
    Nov 21 '18 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 '18 at 15:18











  • I'm sure it has something to do with me using window.showdialog() rather than window.show()

    – Explorex
    Nov 21 '18 at 21:46



















  • I didn't define it

    – Explorex
    Nov 21 '18 at 13:55






  • 1





    I just tried all three, none of which work application continues to run after this window has been closed

    – Explorex
    Nov 21 '18 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 '18 at 15:18











  • I'm sure it has something to do with me using window.showdialog() rather than window.show()

    – Explorex
    Nov 21 '18 at 21:46

















I didn't define it

– Explorex
Nov 21 '18 at 13:55





I didn't define it

– Explorex
Nov 21 '18 at 13:55




1




1





I just tried all three, none of which work application continues to run after this window has been closed

– Explorex
Nov 21 '18 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 '18 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 '18 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 '18 at 15:18













I'm sure it has something to do with me using window.showdialog() rather than window.show()

– Explorex
Nov 21 '18 at 21:46





I'm sure it has something to do with me using window.showdialog() rather than window.show()

– Explorex
Nov 21 '18 at 21:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?