I have the same problem in one of my apps and I solve it by a flag.
private bool isClosingConfirmed = false;
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (!isClosingConfirmed)
{
if (MessageBox.Show("Are you sure you want to close the
application?", "Exit Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.Yes)
{
isClosingConfirmed = true; // Set flag to prevent re-entry
Application.Exit(); // Close the application
}
else
{
e.Cancel = true; // Prevent form from closing
}
}
}