妥善处理文件流 [英] properly disposing of filestream

查看:71
本文介绍了妥善处理文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我收到错误该进程无法访问该文件,因为它正在被另一个进程使用
当我调试程序超过

一次。正在打开该文件以使用FileStream追加访问权限。

我已经使用以下代码实现了IDisposable,我正在做什么

出错了或是否有问题按下停止

调试在这样的情况下结束程序的按钮?


public void Dispose()

{

file1.Close() ;

file2.Close();

file1 = null;

file2 = null;

GC.Collect ();


}


谢谢,

Bob

Hi,
I am getting an error "The process cannot access the file because it
is being used by another process" when I debug a program more than
once. The file is being opened for append access with a FileStream.
I have implemented IDisposable with the following code, am I doing
something wrong or is there a problem with pressing the "stop
debugging" button to end a program in a situation like this?

public void Dispose()
{
file1.Close();
file2.Close();
file1 = null;
file2 = null;
GC.Collect();

}

Thanks,
Bob

推荐答案

这是什么用的?它是否使用使用或类似的?


你可能不需要GC.Collect(),顺便说一下 - 我倾向于

使Dispose()可重复,即


if(file1!= null){

file1.Close();

file1 = null;

}

if(file2!= null){

file2.Close();

file2 = null;

}


现在我可以多次调用Dispose()(只是防守,

)。


Marc

What uses this? Does it use "using" or similar?

You probably don''t need to GC.Collect(), btw - and I would tend to
make Dispose() repeatable, i.e.

if(file1!=null) {
file1.Close();
file1 = null;
}
if(file2!=null) {
file2.Close();
file2 = null;
}

Now I can call Dispose() as many times as I like (just defensive,
really).

Marc


Hello Bob,


AFAIK,你可以''管理这种情况,因为VS调试器会延长

句柄释放不是当你关闭它时,而是当应用程序完成时。


也许有一些选项可以改变这种行为,但我不知道

关于这个


---

WBR,

Michael Nemtsev [.NET / C#MVP] ::博客: http://空格.live.com / laflour


我们大多数人面临的最大危险并不是我们的目标太高而且我们错过了b $ b错过了它,但它太低了我们达到了它 (c)米开朗基罗

BHi,

BI收到错误该过程无法访问该文件因为它

Bis被另一个人使用过程"当我调试一个程序超过

Bonce。正在使用FileStream打开该文件以进行附加访问。

BI使用以下代码实现了IDisposable,我在做什么

Bsomething错误或是否有问题按下停止

Bdebugging在这样的情况下结束程序的按钮?

Bpublic void Dispose()

B {

Bfile1.Close();

Bfile2.Close();

Bfile1 = null;

Bfile2 = null;

BGC.Collect();

B}

B>

BThanks,

BBob
Hello Bob,

AFAIK, u can''t manage with this situation, because the VS debugger prolongs
handle releasing not when u close it, but when app finished.

Maybe there are some options to change this behaviour, but I''m not aware
about this

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
BHi,
BI am getting an error "The process cannot access the file because it
Bis being used by another process" when I debug a program more than
Bonce. The file is being opened for append access with a FileStream.
BI have implemented IDisposable with the following code, am I doing
Bsomething wrong or is there a problem with pressing the "stop
Bdebugging" button to end a program in a situation like this?
Bpublic void Dispose()
B{
Bfile1.Close();
Bfile2.Close();
Bfile1 = null;
Bfile2 = null;
BGC.Collect();
B}
B>
BThanks,
BBob

8月30日晚上11点50分,Marc Gravell< marc.grav ... @ gmail.comwrote:
On Aug 30, 11:50 pm, Marc Gravell <marc.grav...@gmail.comwrote:

是什么用的?它是否使用使用或类似的?


你可能不需要GC.Collect(),顺便说一下 - 我倾向于

使Dispose()可重复,即


if(file1!= null){

file1.Close();

file1 = null;}


if(file2!= null){

file2.Close();

file2 = null;


}


现在我可以多次调用Dispose()(只是防守,

)。


Marc
What uses this? Does it use "using" or similar?

You probably don''t need to GC.Collect(), btw - and I would tend to
make Dispose() repeatable, i.e.

if(file1!=null) {
file1.Close();
file1 = null;}

if(file2!=null) {
file2.Close();
file2 = null;

}

Now I can call Dispose() as many times as I like (just defensive,
really).

Marc



File1和File2是在构造函数中打开的类变量,

意味着持久化。这是为了加快文件访问速度。我通过使用来实验

,但这显着减慢了访问速度。

File1 and File2 are class variables opened in the constructor and
meant to be persistent. This is to speed file access. I experimented
with a using but this slowed access dramatically.


这篇关于妥善处理文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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