Process Active when I close the C # WPF program

0

I have an application in C # it works normally, but when I click the close button, it closes but it does not stop ... I think it gets in some kind of background, ie your process stays active! and it is only finalized when I click on the visual studio Stop. Is there a way to end this process when I click the close button, and why is this error occurring?

When the user runs the program it opens a login screen! if the company is not registered it hides the login screen and opens the company screen! if the company is registered it hides the login screen and opens the menu! will the problem be with regard to hiding the login screen? follow code example

  public Login()
  {
     Inicia();
  }

 private void Inicia() {


      bool ValidaEmpresa = new ConEmpresa().JaEstaCadastrada();
      // Se ValidaEmpresa for true é porque já esta cadastrada!
      if (ValidaEmpresa)
      {
          // Carrega o menu 
          MenuADM frm = new MenuADM();

          this.Hide();

          frm.ShowDialog();
          this.Close();
       }
       else
       {
           Empresa frm = new Empresa(true);
           this.Hide();
           frm.ShowDialog();
           // Apos cadastrar a empresa abre o menu
           MenuADM frm = new MenuADM();
           frm.ShowDialog();
           this.Close();
        }

    }
    
asked by anonymous 02.06.2017 / 17:45

2 answers

1

You yourself answered what the problem is, the home screen is never closed.

The right thing is for you to do this flow control in another class (maybe in the class App , which initializes the application) and, instead of hiding, you need to close form . Just hiding, it will still be available and the app will still be running.

The code would look something like this

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var loginWindow = new LoginWindow();
        loginWindow.Show();

        bool ValidaEmpresa = new ConEmpresa().JaEstaCadastrada();

        if (ValidaEmpresa)
        {
          // Carrega o menu 
          MenuADM frm = new MenuADM();

          loginWindow.Close();

          frm.ShowDialog();
          this.Close();
       }
       else
       {
           Empresa frm = new Empresa(true);
           this.Hide();
           frm.ShowDialog();

           MenuADM frm = new MenuADM();
           frm.ShowDialog();
           loginWindow.Close();
        }
    }
}
    
02.06.2017 / 18:44
0

A medium found that I was able to accomplish this was instead of calling the current way to switch the call using the App.Current, follow the example:

 App.Current.MainWindow = new MenuADM();
 App.Current.MainWindow.Show();
 this.Close();
    
04.06.2017 / 01:50