Dict.CN 在线词典, 英语学习, 在线翻译
Robot·H【移动鼠标,点击生活】
博客园
首页
新随笔
订阅
管理
随笔-177 评论-143 文章-1 trackbacks-1
Dispose的一种是实现
using
System;
using
System.IO;
class
Program
{
static
void
Main()
{
try
{
//
Initialize a Stream resource to pass
//
to the DisposableResource class.
Console.Write(
"
Enter filename and its path:
"
);
string
fileSpec
=
Console.ReadLine();
FileStream fs
=
File.OpenRead(fileSpec);
DisposableResource TestObj
=
new
DisposableResource(fs);
//
Use the resource.
TestObj.DoSomethingWithResource();
//
Dispose the resource.
TestObj.Dispose();
}
catch
(FileNotFoundException e)
{
Console.WriteLine(e.Message);
}
}
}
//
This class shows how to use a disposable resource.
//
The resource is first initialized and passed to
//
the constructor, but it could also be
//
initialized in the constructor.
//
The lifetime of the resource does not
//
exceed the lifetime of this instance.
//
This type does not need a finalizer because it does not
//
directly create a native resource like a file handle
//
or memory in the unmanaged heap.
public
class
DisposableResource : IDisposable
{
private
Stream _resource;
private
bool
_disposed;
//
The stream passed to the constructor
//
must be readable and not null.
public
DisposableResource(Stream stream)
{
if
(stream
==
null
)
throw
new
ArgumentNullException(
"
Stream in null.
"
);
if
(
!
stream.CanRead)
throw
new
ArgumentException(
"
Stream must be readable.
"
);
_resource
=
stream;
_disposed
=
false
;
}
//
Demonstrates using the resource.
//
It must not be already disposed.
public
void
DoSomethingWithResource()
{
if
(_disposed)
throw
new
ObjectDisposedException(
"
Resource was disposed.
"
);
//
Show the number of bytes.
int
numBytes
=
(
int
) _resource.Length;
Console.WriteLine(
"
Number of bytes: {0}
"
, numBytes.ToString());
}
public
void
Dispose()
//
Implement interface
{
Dispose(
true
);
//
Use SupressFinalize in case a subclass
//
of this type implements a finalizer.
GC.SuppressFinalize(
this
);
}
protected
virtual
void
Dispose(
bool
disposing)
{
//
If you need thread safety, use a lock around these
//
operations, as well as in your methods that use the resource.
if
(
!
_disposed)
{
if
(disposing)
{
if
(_resource
!=
null
)
//
_resourec!=null ,_resoure.Dispose();_resoure=null
_resource.Dispose();
Console.WriteLine(
"
Object disposed.
"
);
}
//
Indicate that the instance has been disposed.
_resource
=
null
;
_disposed
=
true
;
}
}
}
posted on 2008-05-09 10:23
Robot·H
阅读(15)
评论(0)
编辑
收藏
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
另存
打印
最新IT新闻:
·
2008年7月25日IT博客精选
·
微软称Windows 7开发很顺利 IE 8年底上市
·
中国SNS捱过严冬 未来十八个月决定SNS生死
·
微软追求雅虎告一段落 与Facebook展开搜索合作
·
百度C2C项目年底上线
博客园新闻频道
博客园首页
社区
一辈子用心做好一件事
我的最新闪存
昨天梦见自己把自己像剥鱼一样给剥了。好恐怖!自己手里拿着菜刀先剃掉自己的头发。呜呜~。。~
7-4 08:46
<
2008年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
与我联系
发短消息
留言簿
给我留言
查看留言
我管理的小组
乱七八圾
我参加的小组
ASP.NET
随笔分类
.NetFramework(6)
Ajax(1)
Ajax
asp.Net2.0(47)
C#2.0(26)
CSS+Javacript(18)
EC(3)
English(1)
Interview(2)
Office Forms Server+SharePoint Server(1)
ProgramLife(20)
Sqlserver(15)
TechBase(9)
UML(3)
XML
每日一句英语(1)
随笔档案
2008年7月 (21)
2008年6月 (26)
2008年5月 (24)
2008年4月 (11)
2008年3月 (8)
2008年1月 (2)
2007年12月 (2)
2007年11月 (5)
2007年10月 (1)
2007年9月 (26)
2007年8月 (21)
2007年7月 (13)
2007年6月 (11)
2007年5月 (3)
2007年4月 (3)
Asp.Net
CodeProject
SilverLight
UML
w3schools
w3school中文
博客堂
说故事、学模式
职场生涯
Company
康盛创想
DB
SqlServer Magazine
SqlServerCenter
EC
艾瑞
创意纪
电子商务博客
English
bjEnglish
China Daily
EnglishTown
i词霸
language_tools
NBC
华尔街英语
JS+CSS+XML+DOM
Ajax基础教程 - 免费试读 - book.csdn.net
最新随笔
1. 电子商务网站
2. SNS 网站
3. [转帖]淘宝开放平台Taobao Open Platform的机会
4. 理财
5. Enterprise Library系列文章回顾与总结
6. Data Access Application block
7. 端口
8. 写技术文档的朋友来着看看
9. 小R每日一句英语
10. DOM:element.addEventListener
11. 35句话,不看你后悔!
12. 你触发了谁?
13. Music_北京欢迎您
14. [转]新三十年目睹之怪现状
15. 字符换行
16. 您是如何读取模板的?
17. 大家讨论一下这个图如何设计程序。
18. [转帖].Net pet shop 4 和 MSMQ
19. ADO.NET Connection Pooling at a Glance
20. 好雨知时节
搜索
积分与排名
积分 - 22816
排名 - 1694
最新评论
1. re: 电子商务网站
@丁学 现在电子商务很火热 csdn的强力推出sns类的"校内网". 电子商务和sns能不能 来一个联姻还是未知数。 ...
--Robot·H
2. re: 电子商务网站
@丁学
改过。
不知道现在还有什么知名的垂直电子商务。
知道的贴上去来。
--Robot·H
3. re: 电子商务网站
淘宝的主域名应该是
--丁学
4. re: 写技术文档的朋友来着看看
@haitian
应该的!
--Robot·H
5. re: 写技术文档的朋友来着看看
@笑疯^_^
昨天写了份概要设计。
--Robot·H
阅读排行榜
1. 【已经找到】准备找工作了[北京地区C#](697)
2. 大家讨论一下这个图如何设计程序。(520)
3. 妓女也玩OCP(413)
4. PetShop介绍集锦(403)
5. 完善了一下一个哥们的面试题(323)
评论排行榜
1. 大家讨论一下这个图如何设计程序。(18)
2. 面试1(11)
3. 真的很无奈(10)
4. 完美的Div弹出窗口(9)
5. 【已经找到】准备找工作了[北京地区C#](9)
60天内阅读排行
1. 大家讨论一下这个图如何设计程序。(520)
2. 完美的Div弹出窗口(318)
3. js值得收藏(241)
4. document.getElementById为空或不是对象的解决方法 (220)
5. JavaScript replace() 方法(197)
Msn:glory_yimart@hotmail.com QQ:2839849 Emal:yimart at 163.com