C#Winform关闭程序后,该过程仍在Windows任务列表管理器上 [英] C# Winform The process still on Windows Task list manager after close programme

查看:204
本文介绍了C#Winform关闭程序后,该过程仍在Windows任务列表管理器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么关闭程序后进程仍在Windows任务列表管理器上?

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

我使用登录Form.cs

i use login Form.cs

 [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }

用户成功登录后,我重定向到另一个母版页

after the user succesuly login, i redirect to another Masterpage

 this.Hide();
            Main_Usr oMainUsr = new Main_Usr();                    
            oMainUsr.Visible = true;

我的伪母版页面是这样的:

my pseudo master page like this:

public Main_Usr()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
        }

当我关闭母版页时,该过程仍在Windows任务列表管理器上. 但是,当我关闭登录页面时,它将终止Windows Task List Manager上的进程.

when i close the masterpage, The process still on Windows Task list manager. But when i close the login page, it kill the process on Windows Task list manager.

是因为我只是隐藏了登录页面? 我必须关闭所有窗口才能真正退出/终止该过程吗?

is that mean because i just hide le login page ? must i close all window to realy quit/kill the process ?

预先感谢您, 史蒂夫

推荐答案

在主窗体关闭时,在winforms中,进程将被杀死.主应用程序形式是Application.Run调用中指定的一种形式.您的情况是登录表单:

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

要关闭表格,您应该调用Close方法.当您调用Hide或将Visibility设置为false时,窗体将保留在内存中.它只是对用户隐藏了.

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.

因此,要获得所需的功能,应将主应用程序形式更改为Main_Usr:

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
}

然后订阅Main_User表单的Load事件.并在事件处理程序中执行以下操作:

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 
    }
}

更新:您可以在Main方法中完成所有操作,就像这样

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
}

但是通常我不会隐藏主表单并将其显示在登录表单下方.因此,在这种情况下,您应该使用Load事件处理程序.由你决定.

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.

顺便说一句,没有母版页和winforms中的页面.所有这些都是针对ASP.NET的.在这里,您有表格:) 还可以考虑命名,例如LoginForm,MainForm等.

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.

这篇关于C#Winform关闭程序后,该过程仍在Windows任务列表管理器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆