C# 了解流及其生命周期(Flush、Dispose、Close)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1864543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding Streams and their lifetime (Flush, Dispose, Close)
提问by Omar
Note: I've read the following two questions already:
注意:我已经阅读了以下两个问题:
Can you explain the concept of streams?
I'm coding in C#
我正在用 C# 编码
- In almost all code samples that use streams, .Dispose(), .Flush(), .Close() are almost always called.
- In the concept of a stream, what does accomplish?
- If I don't dispose a stream that I stored in a variable, is my application leaking somewhere?
- Why do I need to call any of these functions? I've seen code samples that don't do this and still get the job done (without an apparentbreakage)
- 在几乎所有使用流的代码示例中,.Dispose()、.Flush()、.Close() 几乎总是被调用。
- 在流的概念中,完成了什么?
- 如果我不处理存储在变量中的流,我的应用程序是否在某处泄漏?
- 为什么我需要调用这些函数中的任何一个?我见过不这样做但仍能完成工作的代码示例(没有明显的破损)
I'm currently building a class in my application that contains a primary method (lets call it GetStream()
) that returns a stream via myWebRequest.GetResponse().GetResponseStream()
我目前正在我的应用程序中构建一个类,其中包含一个主要方法(让我们调用它GetStream()
),该方法通过以下方式返回流myWebRequest.GetResponse().GetResponseStream()
The primary method GetStream()
returns a Stream
object that can be used for any operation that requires a stream (StreamReader, Bitmap() etc.).
主要方法GetStream()
返回一个Stream
对象,该对象可用于任何需要流的操作(StreamReader、Bitmap() 等)。
Is there a way to have the stream Disposed of automatically after its last use (garbage collected?) without forcing anyone that calls .GetStream()
to manually dispose of it?
有没有办法让流在上次使用后自动处理(垃圾收集?),而不会强迫任何调用.GetStream()
它的人手动处理它?
As you can probably tell, my questions are vague and general. My understanding of streams is not solid, so any links to helpful articles that offer a more in-depth look at streams than a SO question can offer would be appreciated.
正如您可能会说的那样,我的问题含糊不清且笼统。我对流的理解并不扎实,因此任何指向有用文章的链接都将不胜感激,这些文章提供了比 SO 问题可以提供的更深入的流研究。
采纳答案by mqp
Disposing a stream closes it (and probably doesn't do much else.) Closing a stream flushes it, and releases any resources related to the stream, like a file handle. Flushing a stream takes any buffered data which hasn't been written yet, and writes it out right away; some streams use buffering internally to avoid making a ton of small updates to relatively expensive resources like a disk file or a network pipe.
处理流会关闭它(并且可能不会做太多其他事情。)关闭流会刷新它,并释放与流相关的任何资源,例如文件句柄。刷新流会获取任何尚未写入的缓冲数据,并立即将其写出;一些流在内部使用缓冲来避免对相对昂贵的资源(如磁盘文件或网络管道)进行大量小更新。
You need to call either Close
or Dispose
on most streams, or your code is incorrect, because the underlying resource won't be freed for someone else to use until the garbage collector comes (who knows how long that'll take.) Dispose
is preferred as a matter of course; it's expected that you'll dispose all disposable things in C#. You probably don't have to call Flush
explicitly in most scenarios.
你需要调用Close
或Dispose
大部分流,或者你的代码是不正确的,因为底层的资源不会被释放供别人使用,直到垃圾回收器来(谁知道会花多久。) Dispose
优选作为理所当然;预计您将在 C# 中处理所有一次性物品。Flush
在大多数情况下,您可能不必显式调用。
In C#, it's idiomatic to call Dispose
by way of a using
block, which is syntactic sugar for a try-finally block that disposes in the finally, e.g.:
在 C# 中,Dispose
通过using
块的方式调用是惯用的,这是在 finally 中处理的 try-finally 块的语法糖,例如:
using (FileStream stream = new FileStream(path))
{
// ...
}
is functionally identical to
在功能上与
FileStream stream;
try
{
stream = new FileStream(path);
// ...
}
finally
{
if (stream != null)
stream.Dispose();
}