了解流和他们的一生(冲洗,处置,关闭) [英] Understanding Streams and their lifetime (Flush, Dispose, Close)

查看:146
本文介绍了了解流和他们的一生(冲洗,处置,关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:我已经阅读下列两个问题:

Note: I've read the following two questions already:

你能解释一下流的概念

使用流

我编码在C#C#


  1. 在使用流几乎所有的代码样本,.Dispose(),.Flush(),.Close()几乎总是调用。

  2. 在甲流概念,是什么目的?

  3. 如果我不处理,我存储在一个变量流,是我的应用漏水的地方?

  4. 为什么需要调用任何的这些功能​​呢?我见过的代码示例,不这样做,仍然把工作做好(无破损)

  1. In almost all code samples that use streams, .Dispose(), .Flush(), .Close() are almost always called.
  2. In the concept of a stream, what does accomplish?
  3. If I don't dispose a stream that I stored in a variable, is my application leaking somewhere?
  4. 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 apparent breakage)

我目前正在建设我的应用程序类,它包含一个主要方法(可以称之为 GetStream()),它通过 myWebRequest.GetResponse()。GetResponseStream()

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()返回一个可用于任何操作的对象,需要一个流(StreamReader的,位图()等)。

The primary method GetStream() returns a Stream object that can be used for any operation that requires a stream (StreamReader, Bitmap() etc.).

有没有办法有它上次使用(垃圾回收?)不会强迫任何人,调用 .GetStream()手动处置处理完毕后自动流呢?

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?

正如你可能会说,我的问题是含糊和笼统。我流的理解是不牢固的,因此任何链接到有用的文章,提供了一个更深入了解比SO问题,可以提供将不胜感激流。

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.

推荐答案

处置流关闭它(可能没有做太多事情。)关闭一个流刷新它,并释放相关的流中的所有资源,如文件句柄。冲洗小溪还没有被写入还没有任何缓冲的数据,并将其写出马上;一些流使用缓冲内部,以避免一吨的小更新相对昂贵的资源,如磁盘文件或网络管道。

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.

您需要调用关闭的Dispose 上最流,或者你的代码是不正确的,因为底层的资源不会被释放给其他人使用,直到垃圾收集器来(谁知道会花多久。)的Dispose 是首选,因为理所当然的事;它的预期,你会处理掉所有的东西一次性在C​​#中。你可能不必调用刷新明确在大多数情况下。

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.

在C#中,这是惯用的调用的Dispose 使用块,这是一个try-finally块,在最后,如处置语法糖的方法

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.:

using (FileStream stream = new FileStream(path))
{
    // ...
}

在功能上等同

FileStream stream;

try
{
    stream = new FileStream(path);
    // ...
}
finally
{
    if (stream != null)
        stream.Dispose();
}

这篇关于了解流和他们的一生(冲洗,处置,关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆