Active Process when I close the C# WPF program

Asked

Viewed 416 times

0

I have an application in C# it works normally, but when I click the button to close, it closes but it does not stop ... I think it is in some kind of background, ie your process remains active! and it’s only finished when I click on the Stop from the visual studio. Is there any 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! would the problem be with hiding the login screen? follows example of the code

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

    }

2 answers

1


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

The right thing is you do this flow control in another class (maybe in the class App, which is who initializes the application) and, instead of hiding, you need to close the form. Just hiding, it will still be available and the application 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();
        }
    }
}

0

One found way I could 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.