Can't close C# application - c#

I'm developing a C# application and when the user clicks on the X, the application gets minimized inside a trayicon. Like so:
private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
The application is really simple (only one form). The problem is that I can't manage to properly close the application. When the user rights clicks on the tray icon and he chooses "exit" he should be able to close the application. The problem is that even if the tray icon gets unloaded and the form is closed, the application still shows in the Task Manager as an active application. I'm closing the application like this:
private void chiudiToolStripMenuItem_Click(object sender, EventArgs e)
{
trayIcon.Dispose();
this.Close();
Application.Exit();
}
What am I missing here?

I did something similar a while back.
You need to know what is causing the form to close. So when you click on the X, there is a specific reason passed to the FormClosing event. Like so:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
// don't close just yet if we click on x
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
}
Also, I have other code from the context menu Exit click:
private void tsmiExit_Click(object sender, EventArgs e)
{
// close the application forefully
TerminateApplication();
}
/// <summary>
/// Closes the Application.
/// </summary>
private void TerminateApplication()
{
// need to forcefully dispose of notification icon
this.notifyIcon1.Dispose();
// and exit the application
Application.Exit();
}
Edit:
Note: When you click on the X button, the close reason will be a CloseReason.UserClosing. When Application.Exit is called, the FormClosing is called again with a CloseReason.ApplicationExitCall.
End Edit:
Hope this helps
Andez

The e.Cancel = true line in frmChat_FormClosing is blocking the app from shutting down.
You can solve this easily enough by adding a boolean field to your form class, named TerminatingApp. Set this to true before calling this.Close(). Inside frmChat_FormClosing check for the value of TerminatingApp and only set e.Cancel = true if TerminatingApp is false.
Something like this:
private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
{
if (!TerminatingApp)
{
e.Cancel = true;
Hide();
}
}
private void chiudiToolStripMenuItem_Click(object sender, EventArgs e)
{
TerminatingApp = true;
trayIcon.Dispose();
this.Close();
Application.Exit();
}

Ciao, how does it work if you replace Application.Exit with Application.ExitThread ?
I am actually afraid that with your code, when you call this.Close you are getting into the previous method above with the cancelling...

This is where the documentation on Application.Exit() comes in handy (my emphasis in bold):
The Exit method stops all running
message loops on all threads and
closes all windows of the application.
This method does not necessarily force the application to exit. The
Exit method is typically called from
within a message loop, and forces Run
to return. To exit a message loop for
the current thread only, call
ExitThread.

You could call Environment.Exit(0) where the parameter is the exit code.

When you are calling "this.Close" its going to call "frmChat_FormClosing" and in that you are setting "e.Cancel = true" which is creating the problem, use some variable to identify from where close event is being called and set e.Cancel accordingly.

I have tried Environment.Exit(0). It works in this case, and it worked fine for me.

Everyone else is over doing it.
Just do Close();

Related

Stop the execution of FormClosing event at specific condition?

I made multi languages c# application that will switch the language
when user change Language the application must restart in order for the application change the language
now I have one question and one problem
Is there away to change the language without restarting the application ?
a problem showup when Application.Restart(); executed the Formclosing event raised too as shown below and as result the application will not restart
and will promet for the exit message and close if Yes and will a lanche another copy of the application with new language without closing the old one
now.. Is there away the so the Formclosing event not executed in that case only ? or better to have away as I mentioned ad point 1 above.
private void F0100_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result;
result = MessageBox.Show("Are sure you want to exit?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading);
if (result == DialogResult.Yes)
{
Environment.Exit(1);
}
else
{ e.Cancel = true; }
}
If your problem is just to avoid giving the user the possibility to stop the closing when you execute the call to Application.Restart, then all you need to do is to look at the CloseReason passed to your Form_Closing event handler
private void F0100_FormClosing(object sender, FormClosingEventArgs e)
{
// Do not prompt the user if we have called Application.Restart
if(e.CloseReason != CloseReason.ApplicationExitCall)
{
DialogResult result;
....
}
}

Best Way to Close a Win Form

I need to close a form on a button click event.Here in my example I am hiding the form.Think this is not a good way.When I do only Close() the form is disposed forever and need to rerun the programme to retrieve it.
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close(); //closing frmCalender
}
private void frmCalender_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
this.Hide();
e.Cancel = true;
}
}
Give me the best way to close a C# Windows Form.
If you want to close a form, call .Close().
When I do only Close() the form is disposed forever and need to rerun the programme to retrieve it.
When you close the form, I assume you have no references to it. If so, you can create a new copy of your form via the constructor (var form = new MyForm();).
Otherwise, after closing the form, I believe you should be able to call .Show() on it again, as long as something still has a reference to your form.
I think, the best approach would be:
private void buttonClose_Click(object sender, EventArgs e)
{
this.Hide();
}

Detect when a form has been closed c#

I have a WinForm that I create that shows a prompt with a button. This is a custom WinForm view, as a message box dialog was not sufficient.
I have a background worker started and running. I also want to exit the while(aBackgroundWorker.IsBusy) loop if the button on myForm was clicked.
//MyProgram.cs
using(CustomForm myForm = new CustomForm())
{
myForm.Show(theFormOwner);
myForm.Refresh();
while(aBackgroundWorker.IsBusy)
{
Thread.Sleep(1);
Application.DoEvents();
}
}
Right now, in the CustomForm the Button_clicked event, I have
//CustomForm.cs
private void theButton_Click(object sender, EventArgs e)
{
this.Close();
}
Do I need to add more code to the CustomForm class, or the location where I declare and initialize the form in order to be able to detect a closure?
To detect when the form is actually closed, you need to hook the FormClosed event:
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// Do something
}
Alternatively:
using(CustomForm myForm = new CustomForm())
{
myForm.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);
...
}
void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
// Do something
}
You might be going overkill. To show a form like a dialog window and wait for it to exit before returning control back to the calling form, just use:
mySubForm.ShowDialog();
This will "block" the main form until the child is closed.
Make sure your background worker supports cancellation and as others have pointed out use the form closed event handler. This code should point you in the right direction:
using(CustomForm myForm = new CustomForm())
{
myForm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
myForm.Show(theFormOwner);
myForm.Refresh();
while(aBackgroundWorker.IsBusy)
{
Thread.Sleep(1);
Application.DoEvents();
}
}
void ChildFormClosed(object sender, FormClosedEventArgs e)
{
aBackgroundWorker.CancelAsync();
}
Handle the FormClosing event of the form to be notified when the form is closing, so you can perform any cleanup.
You should be able to hook into the FormClosing and FormClosed events.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx
Closing is before it's closed.
Closed is after it's closed.
A couple things...
First, it appears that loop is there in order to prevent execution form proceeding while the dialog is open. If that is the case, change you .Show(parent) to .ShowDialog(parent). That will also take care of the rest of your question.
Note that this.Hide(); is not the same as this.Close(); in the actual dialog your overriding the closed event

Why isnt my windows form closing?

Im playing about with some very simple windows forms. I have an event handler for a form close event that asks the user whether they want to save what they've typed:
private void closeNpForm(object sender, FormClosingEventArgs e)
{
if (!saveFlag)
{
if (MessageBox.Show("Do you want to save the text entered?", "Save Changes?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = true;
saveFlag = true;
writeToFile(this.allText.Text);
}
}
}
if the user clicks yes (indicating they do want to save their text) i call the writeToFile method, and also set a flag so as not to ask them to save again:
private void writeToFile(string text)
{
writer = new StreamWriter("inputdata.txt");
writer.Write(text);
writer.Close();
this.Close();
}
As far as i can see, the writeToFile method should close the form when its finished. But this isnt happening, when i run the writeToFile method, the form just stays open. Can anyone tell me what im doing wrong?
as i understand it, calling this.Close() should trigger a form closing event, calling my event handler, due to the flag now being true, the form should just close without a problem.
note, my parent class extends the Form class, so im just using this to refer to my form instance.
e.Cancel = true -- whoops. The event is told cancel (read: not close the window).
I suspect that because close() is being called from within the close event and there is some internal clobbering going on (either suppressed or the Cancel is propagated over, etc). Just clean up the code (saving to the file has nothing to do with closing the window although the file might be saved and the window closed from within a button event.)
Happy coding.
writing to file and closing the form are two different kinds of operations. you should not have this.Close() in your writeToFile method.
As pst says, by setting e.cancel to true, you are basically telling the CloseForm event to be cancelled, therefore it's not closing once it exits from the closeNpForm event handler.
After exiting closeNpForm, the form checks for the Cancel property of the event and will not actually proceed with closing itself.
Why are you cancelling the close event and then calling writeToFile that closes the form?
In addition to what #pst said, why are you setting Cancel = true if you don't want to cancel the closing of the form?
If you remove e.Cancel = true; and this.Close(); it should do what you want.
This works for me:
public class Form1 : Form
{
bool saveFlag;
private void Form1_Load(object sender, EventArgs ev)
{ FormClosing += closeNpForm;
}
private void closeNpForm(object sender, FormClosingEventArgs e)
{
if (!saveFlag)
{
if (MessageBox.Show("Do you want to save the text entered?", "Save Changes?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = true;
saveFlag = true;
this.Close();
}
}
}
}

WPF hide a window in Closing event prevents from application terminating

A simple question again.
I am using a window in a WPF as a child window, where I would rather have the 'X' button hide the window instead of close. For that, I have:
private void Window_Closing(object sender, CancelEventArgs e) {
this.Hide();
e.Cancel = true;
}
The problem is that when the parent window is closed, this never closes and keeps the app alive.
Is there a clean way to deal with this? I thought of adding a Kill flag to all my user controls (windows):
public bool KillMe;
private void Window_Loaded(object sender, RoutedEventArgs e){
KillMe = false;
}
private void Window_Closing(object sender, CancelEventArgs e) {
this.Hide();
if (!KillMe) e.Cancel = true;
}
Then in MainWindow_Closing() I would have to set all window KillMe flags to true.
Any better way than creating additional flags and forgetting to set them before closing?
You could call Shutdown in the "parent's" closing handler... This will cause your Cancel to be ignored.
From Window.Closing:
If Shutdown is called, the Closing event for each window is raised. However, if Closing is canceled, cancellation is ignored.
I usualy have my own AppGeneral static class for such cases. When I'm realy exiting app I'm setting AppGeneral.IsClosing static bool to true. And then, when closing:
private void Window_Closing(object sender, CancelEventArgs e) {
if (!AppGeneral.IsClosing)
{
this.Hide();
e.Cancel = true;
}
}
Also, you can kill the your own process (that's ugly but working :) ) Process.GetCurrentProcess().Kill();
You should use
Application.Current.Shutdown();
inside your master window closing method.
This should override canceling of any subwindow!

Categories

Resources