All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
xmlHTTP,xmlDOC,与,C#中DataSet的结合,实现AJAX简单示例_.net资料_编程技术
什么样的网页设计是合理的网页设计?_[Html教程]
ASP.NET技巧:一个在移动设备中获取路径的方法_.net资料_编程技术
动态生成DataTable代码_[Asp.Net教程]
C#教程:线程同步
C#中continue语句的使用方法
不妨来做个尝试:UpdatePanel,for,ASP.NET,MVC_[Asp.Net教程]
.Net业务平台的数值精度陷阱与解决方法_.net资料_编程技术
ASP.NET的Request对象的属性介绍_[Asp.Net教程]
ASP.NET,2.0服务器控件之复合控件样式_[Asp.Net教程]
Reading number is top 10 pictures
Flow chart of breast implants
大人物的礼物
9.3阅兵全景图3-外国方阵梯队和坦克方阵梯队
Female model behind the bitterness, often being overcharged3
The money of more than 100 countries and regions1
西游四格漫画(四)
29 the belle stars after bath figure1
关于海盗的研究
girl of HuNan name is LiXiang(湖南辣姐李湘的写真)
战场废物2
Download software ranking
The hero
C#编程思想
传奇私服架设教程
天龙八部十二宫服务端
Boxer's Top ten classic battle7
Proficient in JavaScript
Tram sex maniac 2 (H) rar bag7
matrix1
C#与.NET技术平台实战演练
Boxer Classic video1
归海一刀 published in(发表于) 2014/1/30 1:22:54 Edit(编辑)
asp.net的Close()与Dispose()用法介绍_[Asp.Net教程]

asp.net的Close()与Dispose()用法介绍_[Asp.Net教程]

asp.net的Close()与Dispose()用法介绍_[Asp.Net教程]
很多人都认为Close()方法内部会调用Dispose()方法,所以并没有本质的区别!实际上这个看法不是很准确,对有些类来说,的确Close()和Dispose()没有本质区别,但是对有些类来说并非如此!
首先,让我们看看我们最常使用的SqlConnection的Close()方法和Dispose()方法的区别:
SqlConnection类的Dispose()方法是继承于Component类的,源代码是这样的:
public void Dispose() {
Dispose(true); //调用Dispose的一个带参数的重载
GC.SuppressFinalize(this); //请求系统不要调用指定对象的终结器。
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
lock(this) {
if (site != null && site.Container != null) {
site.Container.Remove(this);
}
if (events != null) {
EventHandler handler = (EventHandler)events[EventDisposed];
if (handler != null) handler(this, EventArgs.Empty);
}
}
}
}
SqlConnection类的Close()方法在MSDN中的说明是这样的:
关闭与数据库的连接。这是关闭任何打开连接的首选方法。 如果 SqlConnection 超出范围,则不会将其关闭。因此,必须通过调用 Close 或 Dispose 显式关闭该连接。Close 和 Dispose 在功能上等效。如果连接池值Pooling 设置为 true 或 yes,则基础连接将返回到连接池。另一方面,如果 Pooling 设置为 false 或 no,则会关闭到服务器的基础连接。
看说明好象是Close()方法和Dispose()方法是类似的,实际上只是在关闭连接这个功能上等效,让我们看看Close ()方法的源代码:
override public void Close() {
IntPtr hscp;
Bid.ScopeEnter(out hscp, " %d#" , ObjectID);
try {
SqlStatistics statistics = null;

RuntimeHelpers.PrepareConstrainedRegions();
try {
#if DEBUG
object initialReliabilitySlotValue = Thread.GetData(TdsParser.ReliabilitySlot);

RuntimeHelpers.PrepareConstrainedRegions();
try {
Thread.SetData(TdsParser.ReliabilitySlot, true);
#endif //DEBUG
statistics = SqlStatistics.StartTimer(Statistics);

// The lock here is to protect against the command.cancel / connection.close

race condition
// The SqlInternalConnectionTds is set to OpenBusy during close, once this

happens the cast below will fail and
// the command will no longer be cancelable. It might be desirable to be

able to cancel the close opperation, but this is
// outside of the scope of Whidbey RTM. See (SqlCommand::Cancel) for other

lock.
lock (InnerConnection) {
InnerConnection.CloseConnection(this, ConnectionFactory);
}
// does not require GC.KeepAlive(this) because of OnStateChange

if (null != Statistics) {
ADP.TimerCurrent(out _statistics._closeTimestamp);
}
#if DEBUG
}
finally {
Thread.SetData(TdsParser.ReliabilitySlot, initialReliabilitySlotValue);
}
#endif //DEBUG
}
catch (System.OutOfMemoryException e) {
Abort(e);
throw;
}
catch (System.StackOverflowException e) {
Abort(e);
throw;
}
catch (System.Threading.ThreadAbortException e) {
Abort(e);
throw;
}
finally {
SqlStatistics.StopTimer(statistics);
}
}
finally {
SqlDebugContext sdc = _sdc;
_sdc = null;
Bid.ScopeLeave(ref hscp);
if (sdc != null) {
sdc.Dispose();
}
}
}
可以看到Close()方法并没有调用Dispose()方法,虽然有一行sdc.Dispose();,但是这只是释放SqlDebugContext

实例,和SqlConnection.Dispose()方法没有关系!

那么区别在哪里呢?
Close()方法只是关闭了连接,然后这个连接被存储到连接池,所以在调用Close()方法以后,还是可以再通过 Open()方法来打开连接的而调用Dispose()方法以后,这个连接就不能在使用了!
还有一个重要区别就是,当Close()方法并没有调用GC.SuppressFinalize(this);,这导致的直接后果就是在垃圾回收的时候需要进行终止化操作,这会导致这个实例的“代龄”提升,从而极大的延迟这个对象的回收时间!
针对SqlConnection这个类来说,如果以后还需要使用这个连接可以使用Close()方法临时关闭连接,如果以后不需要使用这个连接了,可以优先选用Dispose()方法来释放资源,当然你可以使用using关键字来简化这个过程,
OleDbConnection类和OdbcConnection类的源代码我没有找到,但是应该和SqlConnection类是类似的!
让我们在看一个我们常用的类,看看FileStream类的Close()方法和Dispose()方法有什么区别:
FileStream类的Close()方法是继承于Stream类的,源代码是这样的:
public virtual void Close()
{
Dispose(true);
GC.SuppressFinalize(this);
}
FileStream类的Dispose()方法是继承于Stream类的,源代码是这样的:
public void Dispose()
{
Close();
}
是一个标准的Dispose模式的实现,Close()方法调用的是带参数的Dispose方法,然后调用GC.SuppressFinalize(this);请求系统不要调用指定对象的终结器。而Dispose()方法直接调用Close()方法!
对于FileStream类来说,Close()方法和Dispose()方法是没有区别!


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.