使用“using”时出现流关闭错误句法
我将在下面留下我原来的问题。我的问题是为什么以下几行会产生异常
(您可以看到它在 http://www.ideone.com 上运行/rfQLE)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CSQuick
{
class Program
{
static void Main(string[] args)
{
using (var ff = new MemoryStream())
using (var f = new StreamWriter(ff))
{
f.WriteLine("Hi");
using (TextReader ts = new StreamReader(ff))
{
}
}
}
}
}
来自 ideone 网站的异常(可能使用单声道?我正在运行 MSVS 2010 C#,这会导致异常)
Unhandled Exception: System.ObjectDisposedException: The object was used after being disposed.
at System.IO.MemoryStream.CheckIfClosedThrowDisposed () [0x00000] in <filename unknown>:0
at System.IO.MemoryStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at System.IO.StreamWriter.FlushBytes () [0x00000] in <filename unknown>:0
at System.IO.StreamWriter.Flush () [0x00000] in <filename unknown>:0
at System.IO.StreamWriter.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
为什么我收到消息“无法访问关闭的流”的异常。 ?我喜欢做的是使用流编写器编写文本并将最终结果存储在 temp 中。下面应该这样做,但我不明白我的流是如何或为何关闭的?
using (var ff = new MemoryStream())
using (var f = new StreamWriter(ff))
{
foreach (var t in blah)
{
blahblah(t, f);
}
ff.Flush()
using (TextReader ts = new StreamReader(ff))
{
temp = ts.ReadToEnd();
do_a_check(fn, temp);
}
}//Cannot access a closed Stream.
I'll leave my original question below. My question is why does the following lines produce an exception
(you can see it run at http://www.ideone.com/rfQLE)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CSQuick
{
class Program
{
static void Main(string[] args)
{
using (var ff = new MemoryStream())
using (var f = new StreamWriter(ff))
{
f.WriteLine("Hi");
using (TextReader ts = new StreamReader(ff))
{
}
}
}
}
}
Exception from ideone website (possibly using mono? I am running MSVS 2010 C# which causes an exception)
Unhandled Exception: System.ObjectDisposedException: The object was used after being disposed.
at System.IO.MemoryStream.CheckIfClosedThrowDisposed () [0x00000] in <filename unknown>:0
at System.IO.MemoryStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at System.IO.StreamWriter.FlushBytes () [0x00000] in <filename unknown>:0
at System.IO.StreamWriter.Flush () [0x00000] in <filename unknown>:0
at System.IO.StreamWriter.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
Why am i getting a exception with the msg "Cannot access a closed Stream." ? What i like to do is use a streamwriter to write text and store the final results in temp. The below should do that but i dont understand how or why my stream is closed?
using (var ff = new MemoryStream())
using (var f = new StreamWriter(ff))
{
foreach (var t in blah)
{
blahblah(t, f);
}
ff.Flush()
using (TextReader ts = new StreamReader(ff))
{
temp = ts.ReadToEnd();
do_a_check(fn, temp);
}
}//Cannot access a closed Stream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有提供太多信息,但我认为错误是因为当 StreamWriter using 子句完成并且 StreamWriter 关闭时,底层流也被关闭。在这种情况下,当您尝试在 using 语句之后访问流时,您将收到错误“无法访问关闭的流”
You didn't provide much information but I assume that the error is because when the StreamWriter using clause is done and StreamWriter is closed then also the underlying stream is closed as well. In this situation when You'll try to access the stream after the using statement You'll get the error "Cannot access a closed Stream"
发生的情况是,当您处置 TextReader 时,其中的资源(MemoryStream)也会被处置。因此,当 StreamWriter 被释放时,它会刷新,从而写入已释放的 MemoryStream。
您的小重现代码可以更改为刷新:
或者您可以设置
AutoFlush
,这将导致每次写入时刷新流,那么如果您不在后面放置写入行,则不会出现此异常处置:What is happening is that when you dispose the TextReader, the resources within it is disposed as well (the MemoryStream). So when the StreamWriter is disposed, it flushes, thus writing to the disposed MemoryStream.
Your small repro code could be changed to flush:
Or you can set
AutoFlush
which will cause the stream to be flushed each write, then this exception won't be present if you do not put a write line after disposing:您应该显示堆栈跟踪,这会更有用。
截至目前,我认为问题出在 using 块中的
StreamReader
,该块在处理时会关闭读取器和底层流You should show the Stack trace which would be more useful.
As of now i think the problem would be
StreamReader
in the using block which on dispose is closing both the reader and the underlying stream在您的代码中,这
应该自动释放 MemoryStream,添加一些 { } 来扩展范围。或者删除 using 代码并简单地在代码周围添加一个 try catch finally 。
In you're code this
should auto dispose the MemoryStream, add some { } to expand the scope. Or remove the using code and simply add a try catch finally around the code.