gpt4 book ai didi

c# - 在 C# 中捕获关闭时出错

转载 作者:太空狗 更新时间:2023-10-30 00:07:52 25 4
gpt4 key购买 nike

我试图捕捉我的表单的关闭,以便当用户退出时,它会将“用户已退出”保存到文本文件中,这是我的代码:

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;
}
}

我的问题是,我得到这个错误:

"Make sure you do not have an infinite loop or infinite recursion"

关于如何解决这个问题有什么想法吗?

最佳答案

您不能在关闭表单时调用 Close() 方法。删除所有 Close() 调用,它将起作用。

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;
}
}

关于c# - 在 C# 中捕获关闭时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16140297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com