C# Winform The process still on Windows Task list manager after close programme

前端 未结 6 1732
我在风中等你
我在风中等你 2021-01-16 21:46

why The process still on Windows Task list manager after close programme ?

i use login Form.cs

 [STAThread]
        static void Main()
        {
             


        
相关标签:
6条回答
  • 2021-01-16 22:17

    i follow code the @lazyberezovsky and add this on my Login.cs

      private void simpleButton_Valider_Click(object sender, EventArgs e)
            {
           .....
              DialogResult = DialogResult.OK;
                        return;
           .....
       }
    
    0 讨论(0)
  • 2021-01-16 22:23

    This is because the application message loop is associated with your Login form (Application.Run(new Login()) does this), so you need to close the form which started the application to end the process.

    Alternatively, you could just call LoginForm.Show(), before Application.Run, store credentials somewhere and then call Application.Run(new Main_Usr)

    0 讨论(0)
  • 2021-01-16 22:28

    Because Login is the last form of the application to close, you load Main_User only after that - even if Login is hidden it's still actually there. Windows Forms applications are by default configured to exit when the last form closes.

    0 讨论(0)
  • 2021-01-16 22:32

    i found it, i just use the dizlog.

     [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Login oLogin = new Login();
                oLogin.ShowDialog();
                Application.Run(new Main_Usr());
            }
    
    0 讨论(0)
  • 2021-01-16 22:43

    In winforms process will be killed, when main application form is closed. Main application form is one specified in Application.Run call. In your case it is Login form:

    Application.Run(new Login());
    

    To close form you should call Close method. When you call Hide or set Visibility to false, form stays in memory. It just becomes hidden from user.

    So, to achieve desired functionality you should change main application form to Main_Usr:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Main_Usr()); // change main form
    }
    

    Then subscribe to Load event of Main_User form. And in the event handler do following:

    private void Main_User_Load(object sender, EventArgs e)
    {
        using (var loginForm = new Login())
        {
            Hide(); // hide main form
    
            if (loginForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                Close(); // close main form and kill process
                return;
            }
    
            Show(); // show main form if user logged in successfully 
        }
    }
    

    UPDATE: You can do this all in Main method, like this way

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        using(var loginForm = new Login())
             if (loginForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                 return;
        Application.Run(new Main_Usr()); // change main form
    }
    

    but usually I don't hide main form and show it below login form. So, in this case you should use Load event handler. It's up to you.

    BTW there is no masterpages and pages in winforms. This all is for ASP.NET. Here you have forms :) Also consider naming like LoginForm, MainForm etc.

    0 讨论(0)
  • 2021-01-16 22:43
    this.Hide()
    

    doesn`t kill the window.

    So it remains hidden and process remains in memory. this.Close() closes the window and removes its object from memory.

    It is better to do something like this:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var l = new Login();
        l.ShowDialog();
        if(l.Passed)
           Application.Run(new Login());
    }
    

    And implement Passed property inside login window.

    By the way, do you have any multithreading inside? It is another source of errors of this type.

    0 讨论(0)
提交回复
热议问题