问题
why The process still on Windows Task list manager after close programme ?
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;
}
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 ?
Thanks you in advance, Stev
回答1:
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.
回答2:
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)
回答3:
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.
回答4:
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.
回答5:
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());
}
回答6:
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;
.....
}
来源:https://stackoverflow.com/questions/10312364/c-sharp-winform-the-process-still-on-windows-task-list-manager-after-close-progr