惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - 赶路人之刚出发

集成WebSecurity的Authorize进行身份验证时,数据库连接报错问题 Html.ActionLink传递参数 Automapper结合EF实现insert,update方法 MVC中使用RemoteAttribute异步远程验证 Html.RenderPartial WebMatrix.WebSecurity创建自定义用户属性 强类型view中List<Model〉问题 ViewBag任意属性的实现方法 params关键字 配置LINQ中的datacontext的log路径,以记录datacontext执行了的查询sql SortedList LINQ join/left join/cross join/group by/group join/sortedlist/cast Linq to objects示例 yield return 和 Func Lamda表达式 匿名类型与扩展方法 对象初始化器和集合初始化器 C#自动属性 .net random伪随机数
IDisposable
赶路人之刚出发 · 2013-04-19 · via 博客园 - 赶路人之刚出发

.net中所有托管资源均有GC自动回收,那非托管资源怎么办呢?

IDisposable接口就是设计用来显示释放非托管资源,如果是直接调用dispose方法,则无需再经由finalizer方法去调用this.Dispose(false)去释放托管资源,所以需在Dispose()方法中加上

GC.SuppressFinalize(this);
以提醒系统无需再调用析构函数,所有托管、非托管资源均会在Dispose(True)方法中释放,否则仍由Finzlizer方法调用Dispose(false)方法以便释放非托管资源,托管资源由GC直接回收
  public class MyBaseResourceManager : IDisposable
    {
        // Dispose是否被调用
        private bool isDisposed = false;
        protected string Name = "Name";
       
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    //Code to 释放托管资源
                }
                //Code to 释放非托管资源
            }
            isDisposed = true;
        }
        // 析构函数只会在我们没有直接调用Dispose方法的时候调用
        // 派生类中不用在次提供析构函数
        ~MyBaseResourceManager()
        {
            Dispose(false);
        }
    }

    public class MyResourceManager : MyBaseResourceManager
    {
        private bool disposed = false;
      
        protected override void Dispose(bool disposing)
        {
            if (!disposed)
            {
                try
                {
                    if (disposing)
                    {
                        //Code To 释放托管资源
                    }
                    //Code To 释放非托管资源
                }
                finally
                {
                    base.Dispose(disposing);
                }
            }
            
        }
    }

使用Using关键字,即可在using中代码执行结束后自动调用对象mr的dispose方法,如下所示

static void Main()
        {
            using (MyResourceManager mr = new MyResourceManager())
            {
            }

 参见:http://www.cnblogs.com/carysun/archive/2008/06/15/Dispose.html