处置对象 [英] Disposing objects

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

问题描述

我对是否应该处理某些东西感到困惑。

一般规则是什么?你怎么能确定做对的事情?我听说过有关处理非托管资源但听说过什么是非托管资源?


是否所有东西都需要处理?


我一直在使用Webrequest,Webresponse,Streamreaders等。我怎样才能确定需要处理什么?


这些是否自动获取在程序结束时处理

声明它们?


有没有办法检查我的程序,看看我是否有需要的东西

处置并仍在使用资源?


-Jerry

I''m rather confused as to whether something should be disposed of, or not.
What is the general rule? How can you be sure of doing the right thing? I''ve
heard about disposing unmanaged resources but what is an unmanaged resource?

Does everything need disposing?

I''ve been using Webrequest, Webresponse, Streamreaders etc. How can I be
sure of what needs disposing?

Do these get automatically get disposed of at the end of the procedure that
declares them?

Is there any way to check my program to see if I have something that needs
disposing and is still using resources?

-Jerry

推荐答案

每一个实现idisposable接口的对象应该被处理

i建议你使用bloock使用en来编写这些对象

这样的



使用Foo作为新的Foo.Lib


'''''做你的东西


结束使用''最终使用将照顾处理,所以不需要打电话


还注意到一个使用块中声明的变量块获得一个块范围


使用Foo作为新的Foo.Lib

Dim Bla因为字符串''只能在使用区块中访问

'''''做你的东西


结束使用


非共享资源是一种非.Net原生的资源,但通过interop,PInvoke等启动



例如API调用或COM对象


超出范围的对象指针最终将由GC清除
但是如何以及何时发生这种情况只是GC的一个谜知道:-)


hth


Michel Posseth [MCP]
http://www.vbdotnetcoder.com


" Jerry Spence1" < 12*@dfgh.comschreef在bericht

新闻:p9 ***************************** *@posted.plusn et ...
Every object that implements an idisposable interface should be disposed of
i recomend you to write these objects in a using en using bloock

like this

Using Foo as new Foo.Lib

''''''do your stuff

End using ''the end using will take care of disposing so no need to call it

also note that a declared variabel in a using block gets a block scope

Using Foo as new Foo.Lib
Dim Bla As string ''only accessible in the using block
''''''do your stuff

End using

A unmanagaed resource is a resource that is not native to .Net but started
through interop , PInvoke etc etc
for instance API calls or COM objects

Object pointers that go out of scope will eventually be cleaned by the GC
however how and when this happens is a mystery that only the GC knows :-)

hth

Michel Posseth [MCP]
http://www.vbdotnetcoder.com

"Jerry Spence1" <12*@dfgh.comschreef in bericht
news:p9******************************@posted.plusn et...

我对是否应该处理某些东西感到困惑。

一般规则是什么?你怎么能确定做对了?

我听说过处理非托管资源但是什么是非托管资源?b / b资源?

是否所有东西都需要处理?


我一直在使用Webrequest,Webresponse,Streamreaders等。我怎样才能确定是什么?需要处理吗?


这些是否会在程序结束时自动处理

声明它们?


有没有办法检查我的程序,看看我是否有需要的东西

处理并仍在使用资源?


-Jerry
I''m rather confused as to whether something should be disposed of, or not.
What is the general rule? How can you be sure of doing the right thing?
I''ve heard about disposing unmanaged resources but what is an unmanaged
resource?

Does everything need disposing?

I''ve been using Webrequest, Webresponse, Streamreaders etc. How can I be
sure of what needs disposing?

Do these get automatically get disposed of at the end of the procedure
that declares them?

Is there any way to check my program to see if I have something that needs
disposing and is still using resources?

-Jerry



Michel,


你能告诉我你在哪里使用处理方法的最新程序/>
标签?


Cor


" Michel Posseth [MCP]" < MS ** @ posseth.comschreef在bericht

新闻:uD ************* @ TK2MSFTNGP06.phx.gbl ...
Michel,

Can you show me your latest program where you use the dispose method on
Label?

Cor

"Michel Posseth [MCP]" <MS**@posseth.comschreef in bericht
news:uD*************@TK2MSFTNGP06.phx.gbl...

每个实现idisposable接口的对象都应该被处理



i建议你使用bloock使用en来编写这些对象


喜欢这个


使用Foo作为新的Foo.Lib


'''''''做你的东西


结束使用''结束使用将照顾处理所以不需要打电话




还注意到一个使用块中声明的变量块获得一个块范围


使用Foo作为新的Foo.Lib

Dim Bla As string''只能在使用区块中访问

'''''做你的东西


结束使用


A unmanagaed资源是一个非.Net原生的资源,但通过interop,PInvoke等启动



例如API调用或COM对象


超出范围的对象指针最终将被GC清除

然而,这种情况发生的时间和方式只是GC知道:-)


hth


Michel Posseth [MCP]
http://www.vbdotnetcoder.com

$ b" Jerry Spence1" < 12*@dfgh.comschreef在bericht

新闻:p9 ***************************** *@posted.plusn et ...
Every object that implements an idisposable interface should be disposed
of
i recomend you to write these objects in a using en using bloock

like this

Using Foo as new Foo.Lib

''''''do your stuff

End using ''the end using will take care of disposing so no need to call
it

also note that a declared variabel in a using block gets a block scope

Using Foo as new Foo.Lib
Dim Bla As string ''only accessible in the using block
''''''do your stuff

End using

A unmanagaed resource is a resource that is not native to .Net but started
through interop , PInvoke etc etc
for instance API calls or COM objects

Object pointers that go out of scope will eventually be cleaned by the GC
however how and when this happens is a mystery that only the GC knows :-)

hth

Michel Posseth [MCP]
http://www.vbdotnetcoder.com

"Jerry Spence1" <12*@dfgh.comschreef in bericht
news:p9******************************@posted.plusn et...

>我对是否应该处理某些东西感到困惑,或者
不是。一般规则是什么?你怎么能确定做正确的事情?我听说过处理非托管资源但是什么是非托管资源?

是否需要处理所有内容?

我一直在使用Webrequest,Webresponse ,Streamreaders等我怎样才能确定需要处理什么?

这些是否会在声明它们的程序结束时自动处理?
有没有办法检查我的程序,看看我是否有需要处理的东西并且仍在使用资源?

-Jerry
>I''m rather confused as to whether something should be disposed of, or
not. What is the general rule? How can you be sure of doing the right
thing? I''ve heard about disposing unmanaged resources but what is an
unmanaged resource?

Does everything need disposing?

I''ve been using Webrequest, Webresponse, Streamreaders etc. How can I be
sure of what needs disposing?

Do these get automatically get disposed of at the end of the procedure
that declares them?

Is there any way to check my program to see if I have something that
needs disposing and is still using resources?

-Jerry



啊哈,你想要更详细的信息Cor ;-)?


我们都知道在这种情况下调用Dispose不是必需的。



windows窗体或用户控件上放置的所有控件和组件上自动调用Dispose。


但是一般来说规则是


处理所有实现IDisposable的东西

问候

Mic hel


" Cor Ligthert [MVP]" < no ************ @ planet.nlschreef in bericht

news:74 ****************** **************** @ microsof t.com ...
Aha ,, well you wanted more detailed info Cor ;-) ?

As we both know that calling Dispose in that case is not necesary as
Dispose is automatically called on all controls and components placed on
windows forms or user controls.

However in general the rule is

dispose everything that implements IDisposable
regards
Michel

"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:74**********************************@microsof t.com...

Michel,


你能告诉我你最新的程序在哪里使用处理方法

标签?


Cor


Michel Posseth [MCP]" < MS ** @ posseth.comschreef在bericht

新闻:uD ************* @ TK2MSFTNGP06.phx.gbl ...
Michel,

Can you show me your latest program where you use the dispose method on
Label?

Cor

"Michel Posseth [MCP]" <MS**@posseth.comschreef in bericht
news:uD*************@TK2MSFTNGP06.phx.gbl...

>实现idisposable接口的每个对象都应该处理
我建议你使用bloock来编写这些对象

使用Foo作为新的Foo.Lib

'''''做你的东西

结束使用''结束使用会照顾处理所以不需要调用


还要注意使用块中声明的变量块获得块范围

使用Foo作为新的Foo.Lib
Dim Bla作为字符串''只能在使用块中访问
'''''做你的东西

结束使用

非文本资源是一种非.Net原生的资源,但是通过互操作,PInvoke等启动,例如API调用或COM对象

对象指针范围最终将由GC清除如何以及何时发生这是一个只有GC知道的谜:





Michel Posseth [MCP]
http://www.vbdotnetcoder.com


" Jerry Spence1" < 12*@dfgh.comschreef在bericht
新闻:p9 ****************************** @发布。加上净...
>Every object that implements an idisposable interface should be disposed
of
i recomend you to write these objects in a using en using bloock

like this

Using Foo as new Foo.Lib

''''''do your stuff

End using ''the end using will take care of disposing so no need to call
it

also note that a declared variabel in a using block gets a block scope

Using Foo as new Foo.Lib
Dim Bla As string ''only accessible in the using block
''''''do your stuff

End using

A unmanagaed resource is a resource that is not native to .Net but
started through interop , PInvoke etc etc
for instance API calls or COM objects

Object pointers that go out of scope will eventually be cleaned by the GC
however how and when this happens is a mystery that only the GC knows
:-)

hth

Michel Posseth [MCP]
http://www.vbdotnetcoder.com

"Jerry Spence1" <12*@dfgh.comschreef in bericht
news:p9******************************@posted.plus net...

>>我对是否应该处理某些东西感到困惑,或者不是。一般规则是什么?你怎么能确定做正确的事情?我听说过处理非托管资源但是什么是非托管资源?

是否需要处理所有内容?

我一直在使用Webrequest,Webresponse ,Streamreaders等我怎样才能确定需要处理什么?

这些是否会在声明它们的程序结束时自动处理?
有没有办法检查我的程序,看看我是否有需要处理的东西并且仍在使用资源?

-Jerry
>>I''m rather confused as to whether something should be disposed of, or
not. What is the general rule? How can you be sure of doing the right
thing? I''ve heard about disposing unmanaged resources but what is an
unmanaged resource?

Does everything need disposing?

I''ve been using Webrequest, Webresponse, Streamreaders etc. How can I be
sure of what needs disposing?

Do these get automatically get disposed of at the end of the procedure
that declares them?

Is there any way to check my program to see if I have something that
needs disposing and is still using resources?

-Jerry




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

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