I'm trying to catch the close of my form so that when a user exits, it saves "User has exited" to a text file, this is my code:
private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (String.IsNullOrEmpty(directory))
{
Close();
e.Cancel = false;
}
else
{
string time = DateTime.Now.ToString("hh:mm");
TextWriter msg = new StreamWriter(directory, true);
msg.WriteLine(" (" + time + ") == " + uName + " Has Left The Chat == ");
msg.Close();
Close();
e.Cancel = false;
}
}
My problem is, I get this error:
"Make sure you do not have an infinite loop or infinite recursion"
Any ideas on how to fix this?
You cannot call the Close() method from form closing. Remove all the Close() calls and it will work.
private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (String.IsNullOrEmpty(directory))
{
e.Cancel = false;
}
else
{
string time = DateTime.Now.ToString("hh:mm");
using(TextWriter msg = new StreamWriter(directory, true))
{
msg.WriteLine(" (" + time + ") == " + uName + " Has Left The Chat == ");
msg.Close();
}
e.Cancel = false;
}
}
You don't need to be calling the Close()
method. Someone already called it, if the mainForm_FormClosing
event was executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With