網(wǎng)站首頁 編程語言 正文
一、常見非托管資源
- Windows窗口句柄、數(shù)據(jù)庫鏈接、GDI對(duì)象、獨(dú)占文件鎖等等對(duì)象
- ApplicationContext,Brush,Component,ComponentDesigner,Container,Context,Cursor
- FileStream,Font,Icon,Image,Matrix,Object,OdbcDataReader,OleDBDataReader,Pen
- Regex,Socket,StreamWriter,Timer,Tooltip
二、實(shí)現(xiàn)原理
定義類繼承IDisposable
接口
public class DisposeTest : IDisposable { //實(shí)現(xiàn)IDisposable接口方法 public void Dispose() { } }
在類中定義資源和方法
public class DisposeTest : IDisposable { #region Dispose經(jīng)典實(shí)現(xiàn)方式 //實(shí)現(xiàn)IDisposable接口方法 public void Dispose() { Dispose(true);//調(diào)用處理方法 GC.SuppressFinalize(this);//讓GC忽略 } //資源 private readonly IntPtr unmanagedResource;//非托管內(nèi)存 private readonly SafeHandle managedResource;//托管資源 //構(gòu)造方法初始化資源 public DisposeTest() { unmanagedResource = Marshal.AllocHGlobal(sizeof(int));//分配非托管內(nèi)存 managedResource = new SafeFileHandle(new IntPtr(), true);//創(chuàng)建托管資源 } //根據(jù)不同資源進(jìn)行處理 protected virtual void Dispose(bool isManualDisposing) { ReleaseUnmanagedResourse(unmanagedResource);//處理非托管資源 if (isManualDisposing) { ReleaseManagedResources(managedResource);//處理托管資源 } } private void ReleaseUnmanagedResourse(IntPtr intPtr) { Marshal.FreeHGlobal(intPtr); //釋放非托管內(nèi)存 } private void ReleaseManagedResources(SafeHandle safeHandle) { if (safeHandle != null) { safeHandle.Dispose(); //釋放托管資源 } } //析構(gòu)函數(shù)處理托管資源 ~DisposeTest() { Dispose(false); } #endregion }
三、Close()和Dispose()區(qū)別
- lose()方法關(guān)閉對(duì)象,沒有完全釋放。Dispose()方法完全釋放了
- 一般情況,Close() 實(shí)現(xiàn)了接口對(duì)Dispose()的封裝,調(diào)用時(shí)不需要直接調(diào)用Dispose()
四、常見封裝的語法寫法
Close()方法
SqlConnection conn3 = new SqlConnection(); try { conn3.Open(); } catch (Exception) { throw; } finally { conn3.Close();//關(guān)閉對(duì)象 }
using()
using (SqlConnection cnn = new SqlConnection()) { //此處使用,使用完后系統(tǒng)會(huì)自動(dòng)釋放 }
Dispose()
SqlConnection cnn2 = new SqlConnection(); try { //這里寫要執(zhí)行的代碼 } finally { cnn2.Dispose(); }
原文鏈接:https://www.cnblogs.com/wml-it/p/16081656.html
相關(guān)推薦
- 2022-05-12 Ubuntu 20.04開啟root用戶并恢復(fù)rc.local開機(jī)自啟
- 2022-12-09 Python曲線擬合多項(xiàng)式深入詳解_python
- 2022-11-10 android時(shí)間選擇控件之TimePickerView使用方法詳解_Android
- 2023-01-28 C#實(shí)現(xiàn)自定義單選和復(fù)選按鈕樣式_C#教程
- 2022-07-21 python:實(shí)現(xiàn)balanced parentheses平衡括號(hào)表達(dá)式算法(附完整源碼)
- 2022-10-22 react實(shí)現(xiàn)Modal彈窗效果_React
- 2022-06-24 React如何使用refresh_token實(shí)現(xiàn)無感刷新頁面_React
- 2022-07-08 python中的annotate函數(shù)使用_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支