C# “c”;无法写入已关闭的TextWriter。”;在字符串对象上?
许多用户都看到了这个错误,但在我的例子中,VisualStudio似乎指向了一个字符串对象。 我的代码如下:C# “c”;无法写入已关闭的TextWriter。”;在字符串对象上?,c#,textwriter,C#,Textwriter,许多用户都看到了这个错误,但在我的例子中,VisualStudio似乎指向了一个字符串对象。 我的代码如下: protected delegate void DPrint_To_LogScreen(string Text, bool NewLine); protected void Print_To_LogScreen(string Text, bool NewLine) { if (InvokeRequired) Invoke(new DPrint_To_LogScr
protected delegate void DPrint_To_LogScreen(string Text, bool NewLine);
protected void Print_To_LogScreen(string Text, bool NewLine)
{
if (InvokeRequired)
Invoke(new DPrint_To_LogScreen(Print_To_LogScreen), new object[] { Text, NewLine }); // exception thrown here from the Text string
else
{
LogScreen.AppendText(Convert.ToString(DateTime.Now) + " -> " + Text + (NewLine ? System.Environment.NewLine : ""));
if (Log_Screen_File == null)
{
Log_Screen_File = new StreamWriter(@"Global.log", true);
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
Log_Screen_File.Close();
}
else
{
lock (Log_Screen_File)
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
}
}
我通常想从不同的地方和线程调用函数Print_to_LogScreen
我期望“if(Log\u Screen\u File==null)”语句可以完成这项工作(在一般情况下它可以工作),但现在调用命令上的文本对象引发了异常
这是可能的还是VisualStudio意味着输出文件?如果是这样,为什么的“if(Log\u Screen\u File==null)”不起作用
谢谢呼叫
Close
不会将其设置为null
。此外,您应该在此处使用使用。将代码更改为:
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
它的功能与上面的代码相同,只是它不会引发您当前遇到的异常
不过,很难说你真正想要发生什么。在我看来,你似乎有一个潜在的问题。假设线程A和线程B正在执行。线程A看到Log\u Screen\u File==null
,并创建它。然后线程B获得一个时间片,并看到该文件存在。然后线程A获得另一个时间片,写入文件并关闭它。线程B将尝试写入一个不存在的文件
如果此代码将由多个线程使用,则必须确保整个操作是原子的。我建议:
private readonly object logLock = new object();
protected void Print_To_LogScreen(string Text, bool NewLine)
{
if (InvokeRequired)
Invoke(new DPrint_To_LogScreen(Print_To_LogScreen), new object[] { Text, NewLine }); // exception thrown here from the Text string
else
{
lock (logLock)
{
LogScreen.AppendText(Convert.ToString(DateTime.Now) + " -> " + Text + (NewLine ? System.Environment.NewLine : ""));
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
else
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
}
}
}
但是你真的想每次都打开和关闭文件吗?你会不会这样做:
if (Log_Screen_File == null)
{
Log_Screen_File = new StreamWriter(@"Global.log", true);
}
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
当然,假设您在程序退出时关闭日志文件
仔细想想,您可能根本不需要锁,因为该方法是在UI线程上执行的。不过,锁不会伤害任何东西,也不会影响性能。调用Close
不会将其设置为null
。此外,您应该在此处使用使用。将代码更改为:
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
它的功能与上面的代码相同,只是它不会引发您当前遇到的异常
不过,很难说你真正想要发生什么。在我看来,你似乎有一个潜在的问题。假设线程A和线程B正在执行。线程A看到Log\u Screen\u File==null
,并创建它。然后线程B获得一个时间片,并看到该文件存在。然后线程A获得另一个时间片,写入文件并关闭它。线程B将尝试写入一个不存在的文件
如果此代码将由多个线程使用,则必须确保整个操作是原子的。我建议:
private readonly object logLock = new object();
protected void Print_To_LogScreen(string Text, bool NewLine)
{
if (InvokeRequired)
Invoke(new DPrint_To_LogScreen(Print_To_LogScreen), new object[] { Text, NewLine }); // exception thrown here from the Text string
else
{
lock (logLock)
{
LogScreen.AppendText(Convert.ToString(DateTime.Now) + " -> " + Text + (NewLine ? System.Environment.NewLine : ""));
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
else
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
}
}
}
但是你真的想每次都打开和关闭文件吗?你会不会这样做:
if (Log_Screen_File == null)
{
Log_Screen_File = new StreamWriter(@"Global.log", true);
}
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
当然,假设您在程序退出时关闭日志文件
仔细想想,您可能根本不需要锁,因为该方法是在UI线程上执行的。不过,锁不会伤害任何东西,也不会影响性能。调用Close
不会将其设置为null
。此外,您应该在此处使用使用。将代码更改为:
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
它的功能与上面的代码相同,只是它不会引发您当前遇到的异常
不过,很难说你真正想要发生什么。在我看来,你似乎有一个潜在的问题。假设线程A和线程B正在执行。线程A看到Log\u Screen\u File==null
,并创建它。然后线程B获得一个时间片,并看到该文件存在。然后线程A获得另一个时间片,写入文件并关闭它。线程B将尝试写入一个不存在的文件
如果此代码将由多个线程使用,则必须确保整个操作是原子的。我建议:
private readonly object logLock = new object();
protected void Print_To_LogScreen(string Text, bool NewLine)
{
if (InvokeRequired)
Invoke(new DPrint_To_LogScreen(Print_To_LogScreen), new object[] { Text, NewLine }); // exception thrown here from the Text string
else
{
lock (logLock)
{
LogScreen.AppendText(Convert.ToString(DateTime.Now) + " -> " + Text + (NewLine ? System.Environment.NewLine : ""));
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
else
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
}
}
}
但是你真的想每次都打开和关闭文件吗?你会不会这样做:
if (Log_Screen_File == null)
{
Log_Screen_File = new StreamWriter(@"Global.log", true);
}
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
当然,假设您在程序退出时关闭日志文件
仔细想想,您可能根本不需要锁,因为该方法是在UI线程上执行的。不过,锁不会伤害任何东西,也不会影响性能。调用Close
不会将其设置为null
。此外,您应该在此处使用使用。将代码更改为:
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
它的功能与上面的代码相同,只是它不会引发您当前遇到的异常
不过,很难说你真正想要发生什么。在我看来,你似乎有一个潜在的问题。假设线程A和线程B正在执行。线程A看到Log\u Screen\u File==null
,并创建它。然后线程B获得一个时间片,并看到该文件存在。然后线程A获得另一个时间片,写入文件并关闭它。线程B将尝试写入一个不存在的文件
如果此代码将由多个线程使用,则必须确保整个操作是原子的。我建议:
private readonly object logLock = new object();
protected void Print_To_LogScreen(string Text, bool NewLine)
{
if (InvokeRequired)
Invoke(new DPrint_To_LogScreen(Print_To_LogScreen), new object[] { Text, NewLine }); // exception thrown here from the Text string
else
{
lock (logLock)
{
LogScreen.AppendText(Convert.ToString(DateTime.Now) + " -> " + Text + (NewLine ? System.Environment.NewLine : ""));
if (Log_Screen_File == null)
{
using (Log_Screen_File = new StreamWriter(@"Global.log", true))
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
Log_Screen_File = null;
}
else
{
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
}
}
}
}
但是你真的想每次都打开和关闭文件吗?你会不会这样做:
if (Log_Screen_File == null)
{
Log_Screen_File = new StreamWriter(@"Global.log", true);
}
Log_Screen_File.WriteLine(Convert.ToString(DateTime.Now) + " -> " + Text);
当然,假设您在程序退出时关闭日志文件
仔细想想,您可能根本不需要锁,因为该方法是在UI线程上执行的。不过,锁不会伤害任何东西,也不会影响性能。在您的脑海中-调用该方法两次。第二次通过时,Log\u Screen\u File
的值是多少?提示:它不是null
,并且它已经在您的头脑中接近-调用该方法两次。第二次通过时,Log\u Screen\u File
的值是多少?提示:它不是null
,并且它已经在您的头脑中接近-调用该方法两次。Log的值是多少