I'm beginner in C# and I was trying to make WFA application. But there is one problem. When I run it, it shows first(login) form and if I close it without logging in, it will works fine, but when i login, first form hides, secound form shows up and if i close it by clicking X button, form disappeared but I can see process running in task manager. On secound form is also logout button, and if i click button it hides secound form, and shows first(login) form, and if i close it, still process running in background.
How can i stop process running in background when closing App by X button?
I was searching but i couldn't find solution for my problem. (i found something to attach to process and see threads but there is too many of them)
There is code where Forms are created hided etc:
If logged in frmLogin hides -> show frmMail
If logged out frmMail hides -> show frmLogin.
If user successfully logged in:
this.Hide();
frmMail f2 = new frmMail();
f2.Show();
If user click "Logout" button:
this.Hide();
frmLogin f1 = new frmLogin();
f1.Show();
I found solution, if everyone have same problem this may help.
How to switch between forms without creating new instance of forms?
How do I prevent the app from terminating when I close the startup form?
What I will do in your case is pretty simple.
Fist I will override the FormClose_Event, something like this
void Form_FormClosing(object sender, FormClosingEventArgs e)
{
//And here you can call
Application.Exit();
// which this will cause to close everything in your application
//Also if you want to be more aggressive, there is another option, you can
//use, Environment.Exit(1), which will basically kill you process.
}
I hope my answer helps you, have a nice day!
Related
Form2 is user confirmation window,i want to close the application by stop button on mainform,but currently form2 is active and there is no dependency between form2 and mainform.How to close application ?
You must be opening Form2 using ShowDialog() method. Instead of that use Show() method and on MainForm button's click event call Application.Exit() method
You can use...
// for WinForms application
if (System.Windows.Forms.Application.MessageLoop)
{
System.Windows.Forms.Application.Exit();
}
// for Console application
System.Environment.Exit(1);
So your problem is the parent window will not have focus/input until the modal window (the confirmation dialog) has finished.
So, to avoid this, just make the confirmation dialog using a custom window (instead of a common dialog / msgbox) and show it. This way if the user wants to click your main form they can (the confirmation will be on background, covered by the main window because of this).
it looks like you're showing the dialog as ShowDialog() or showing a built in MessageBox which is also a ShowDialog that needs to be closed in order for focus to return back to main window. So what you should do is create a custom Dialog and use dialog.Show() method. This will allow you to interact with your main window while the dialog is open and you can click on a button to close your application regardless of the dialog being open or not. Following code should do the closing work for you
Application.Current.ShutDown();
I have crated a windows form application.
VS2012 created a form1 form to me
then I crated a second form called Main
there is a button on form1 form to go to Main form
when I run the application using vs2012, and then I click the x button at the top right of form form1, the form close and I can start working on vs. but the problem is when I click the x button at the top right of the form Main, I can't start working on VS, but I need to click Stop Debugging red button in the vs in order to be able to type in vs.
so my question is when I give this application to my customers, and when the click the x button at the top right of the form, I need the application to close totally, I don't need them to go the task manager and close the application from it.
Your application starts with Form 1, when you close it you are closing the application. Main is just a form, when you close it you are closing the form. What you need to is, go to FormClosing event of Main form and add
Application.Exit();
got to the FormClosing event and put this code :
System.Envirenement.Exit(0);
Problem is because your Form1 form still running(hidden).
If you use Form1 only for open your Main form. Then delete Form1 and set Main as StartUp form in Properties of your proejct:
Application -> StartUp form
After this when you close Main form application will be closed properly
If you use Form1 then you need close Form1 too after you close Main form.
Use FormClosed eventhandler for this purpose
What do you do with form1 when you go to Main? If you just hide it in order to show the other, then close the other one, while just keeping form1 hidden, then form1 will naturally still be running, even if Main is closed.
Sounds like the logical thing to do would be to go back to form1 from Main when you close the latter - otherwise, you can close both using Application.Exit(0) on the appropriate event.
In either case, it would be easier to answer this if you had provided some actual code.
Update:
You could also use System.Environment.Exit(0), but that might require more privileges, which might or might not affect your situation. Again - some code would be helpful.
In regard to your comments, see this answer for more info about termination options.
Right click on Main form -> Properties
on the Event tab choose FormClosing event
and write this code
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(0);
}
I'm encountering strange behaviour from my WinForms app in VS 2010. I launch a new form using straight-forward code:
MainDisplayForm.cs:
using (MyForm myForm = new MyForm())
{
var result = myForm.ShowDialog();
if (result == DialogResult.OK)
{
// do stuff
}
}
I added a Cancel button to MyForm that displayed a confirmation MessageBox to the user and then called this.Close(). I later removed the this.Close() line because I added a dedicated Close button. However, whenever I press Cancel the instance of MyForm still closes!
MyForm.cs:
private void cmdCancel_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show( ... )
// clear the form if user really wants to quit
// this.Close()
// even after removing the above line, program still jumps to FormClosing
}
When I debug line-by-line after clicking on the Cancel button, the program flow just jumps to MyForm_FormClosing after it hits the end of cmdCancel_Click. I created a new button and set its click event to cmdCancel_Click and it did not close the form - so the problem is solved, but I am still wondering if this is just a bug, or something else? I also made sure to reset the DialogResult property of my Cancel button back to None (after changing it to Cancel before I introduced the dedicated Close button).
When exiting the scope of 'using' statement, it calls 'myForm.Dispose()' (that's the whole point of 'using' - to make sure Dispose()is called). This in turn destroys 'myForm' instance, including closing the window.
I have a problem with modality of the forms under C#.NET. Let's say I have main form #0 (see the image below). This form represents main application form, where user can perform various operations. However, from time to time, there is a need to open additional non-modal form to perform additional main application functionality supporting tasks. Let's say this is form #1 in the image. On this #1 form there might be opened few additional modal forms on top of each other (#2 form in the image), and at the end, there is a progress dialog showing a long operation progress and status, which might take from few minutes up to few hours. The problem is that the main form #0 is not responsive until you close all modal forms (#2 in the image). I need that the main form #0 would be operational in this situation. However, if you open a non-modal form in form #2, you can operate with both modal #2 form and newly created non modal form. I need the same behavior between the main form #0 and form #1 with all its child forms. Is it possible? Or am I doing something wrong? Maybe there is some kind of workaround, I really would not like to change all ShowDialog calls to Show...
Image http://img225.imageshack.us/img225/1075/modalnonmodalproblem.png
Modal forms do exactly what "modal" means, they disable all other windows in the app. That's rather important, your program is in a somewhat perilous state. You've got a chunk of code that is waiting for the dialog to close. Really Bad Things could happen if those other windows were not disabled. Like the user could start the modal dialog again, now your code is nested twice. Or she could close the owner window of the dialog, now it suddenly disappears.
These are the exact kind of problems you'd run into if you call Application.DoEvents() inside a loop. Which is one way to get a form to behave modal without disabling other windows. For example:
Form2 mDialog;
private void button1_Click(object sender, EventArgs e) {
mDialog = new Form2();
mDialog.FormClosed += (o, ea) => mDialog = null;
mDialog.Show(this);
while (mDialog != null) Application.DoEvents();
}
This is dangerous.
It is certainly best to use modal forms the way they were designed to stay out of trouble. If you don't want a modal form then simply don't make it modal, use the Show() method. Subscribe to its FormClosing event to know that it is about to close:
private void button1_Click(object sender, EventArgs e) {
var frm = new Form2();
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
frm.Show();
}
void frm_FormClosing(object sender, FormClosingEventArgs e) {
var frm = sender as Form2;
// Do something with <frm>
//...
}
The first thing that comes to mind would be something like this. You could disable form 1 when you launch form 2 and then have form 1 handle the closed event of the second form to re-enable itself. You would NOT open modal 2 using show dialog.
Now keep in mind, from a user perspective this is going to be quite cumbersome, you might look at doing a MDI application to get all windows inside of a single container.
Your main form will not be responsive until any modal dialogs that are in the same process space are closed. There is not work around for that.
It looks to me like you could use an MDI application setting the Form #0 IsMdiContainer property to true.
Then, you could do something alike:
public partial class Form0 {
public Form0 {
InitializeComponent();
this.IsMdiContainer = true; // This will allow the Form #0 to be responsive while other forms are opened.
}
private void button1_Click(object sender, EventArgs e) {
Form1 newForm1 = new Form1();
newForm1.Parent = this;
newForm1.Show();
}
}
Using the ShowDialog() as you stated in your question will make all of the forms Modal = true.
By definition, a modal form is:
When a form is displayed modally, no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (usually in response to some user action) before input to another form can occur. Forms that are displayed modally are typically used as dialog boxes in an application.
You can use this property [(Modal)] to determine whether a form that you have obtained from a method or property has been displayed modally.
So, a modal form shall be used only when you require immediate assistance/interaction from the user. Using modal forms otherwise makes believe that you're perhaps running into a wrong direction.
If you do not want your main form to be an MDI container, then perhaps using multithreading is one solution through a simple BackgroundWorker class is the key to what you want to achieve. Thus, it looks to me like a design smell...
What is it you want to do, apart of making your main form responsive, etc.
What is it you have to do?
Explaining what you have to do, we might be able to guide you altogether into the right, or at least perhaps better, direction.
Actually the answer is very simple. Try
newForm.showDialog();
This will open a new form, while the parent one is inaccessible.
I want a help. I have a window form application. Whenever I click on the "close" of the form, the application should itself close.
Can anyone help me.
Regards,
Justin Samuel.
After your explanation:
In Form1, do something like:
Form2 f2 = new Form2();
f2.ShowDialog();
this.Close();
Application.Exit();
This is assuming Form2 can be shown Modal (Dialog), which I think is correct
A Winforms application will exit automatically when the main form is closed. The main form is the form that is instantiated and passed to Application.Run method in the Main method of the application.
If the application process does not exit when this form is closed, something is preventing it from closing, such as a thread (that is not a background thread) that is performing some work.
By your description, it sounds like you may have multiple forms in your application and one form is still running even if its's not visible. In this case, the close button, would close the form, but not exit the application.
You need to add an event handler for the Form_FormClosed event and then call Application.Exit() - Ideally, you could also handle the Form_FormClosing event if you want to give the user a dialog to ask if they really meant to close.
Maybe you are looking for the Application.Exit() method.