自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析C# Dispose方法的實現(xiàn)

開發(fā) 后端
C# Dispose方法是什么呢?C# Dispose方法的實現(xiàn)是如何的呢?實現(xiàn)的過程中應該注意些什么呢?那么本文就向你介紹到這里,希望對你了解和學習C# Dispose方法有所幫助。

C# Dispose方法的理解是什么呢?類型的Dispose方法應釋放它擁有的所有資源。它還應該通過調用其父類型的Dispose方法釋放其基類型擁有的所有資源。該父類型的 Dispose 方法應該釋放它擁有的所有資源并同樣也調用其父類型的 Dispose 方法,從而在整個基類型層次結構中傳播此模式。若要確保始終正確地清理資源,Dispose 方法應該可以被多次調用而不引發(fā)任何異常。

Dispose 方法應該為它處置的對象調用 GC.SuppressFinalize 方法。如果對象當前在終止隊列中,GC.SuppressFinalize 防止其 Finalize 方法被調用。請記住,執(zhí)行 Finalize 方法會大大減損性能。如果您的 Dispose 方法已經(jīng)完成了清理對象的工作,那么垃圾回收器就不必再調用對象的 Finalize 方法。

C# Dispose方法的實現(xiàn)時注意

為 System.GC.KeepAlive(System.Object) 方法提供的代碼示例演示了強行垃圾回收如何在回收對象的成員仍在執(zhí)行時引起終結器運行。在較長的Dispose方法末尾***調用KeepAlive方法。

下面的代碼示例旨在闡釋用于為封裝了非托管資源的類實現(xiàn)Dispose方法的建議設計模式。整個 .NET Framework 中都實現(xiàn)了此模式。

資源類通常是從復雜的本機類或 API 派生的,而且必須進行相應的自定義。使用這一代碼模式作為創(chuàng)建資源類的一個起始點,并根據(jù)封裝的資源提供必要的自定義。不能編譯該示例,也不能將其直接用于應用程序。

在此示例中,基類 BaseResource 實現(xiàn)可由該類的用戶調用的公共 Dispose 方法。而該方法又調用 virtual Dispose(bool disposing) 方法(Visual Basic 中為 virtual Dispose(disposing As Boolean))。根據(jù)調用方的標識傳遞 true 或 false。以虛 Dispose 方法為對象執(zhí)行適當?shù)那謇泶a。

Dispose(bool disposing) 以兩種截然不同的方案執(zhí)行。如果 disposing 等于 true,則該方法已由用戶的代碼直接調用或間接調用,并且可釋放托管資源和非托管資源。如果 disposing 等于 false,則該方法已由運行庫從終結器內部調用,并且只能釋放非托管資源。因為終結器不會以任意特定的順序執(zhí)行,所以當對象正在執(zhí)行其終止代碼時,不應引用其他對象。如果正在執(zhí)行的終結器引用了另一個已經(jīng)終止的對象,則該正在執(zhí)行的終結器將失敗。

基類提供的 Finalize 方法或析構函數(shù)在未能調用 Dispose 的情況下充當防護措施。Finalize 方法調用帶有參數(shù)的 Dispose 方法,同時傳遞 false。不應在 Finalize 方法內重新創(chuàng)建 Dispose 清理代碼。調用 Dispose(false) 可以優(yōu)化代碼的可讀性和可維護性。

類 MyResourceWrapper 闡釋如何使用 Dispose 從實現(xiàn)資源管理的類派生。MyResourceWrapper 重寫 virtual Dispose(bool disposing) 方法并為其創(chuàng)建的托管和非托管資源提供清理代碼。MyResourceWrapper 還對其基類 BaseResource 調用 Dispose 以確保其基類能夠適當?shù)剡M行清理。請注意,派生類 MyResourceWrapper 沒有不帶參數(shù)的 Finalize 方法或 Dispose 方法,因為這兩個方法從基類 BaseResource 繼承這些參數(shù)。

C# Dispose方法的實現(xiàn)時注意

此示例中的 protected Dispose(bool disposing) 方法不強制線程安全,因為無法從用戶線程和終結器線程同時調用該方法。另外,使用 BaseResource 的客戶端應用程序應從不允許多個用戶線程同時調用 protected Dispose(bool disposing) 方法。應用程序或類庫的設計原則為:應用程序或類庫應只允許一個線程擁有資源的生存期,并且應在不再需要資源時調用 Dispose。根據(jù)資源的不同,在處置資源時進行異步線程訪問可能會帶來安全風險。開發(fā)人員應仔細檢查自己的代碼,以確定***的方法來強制線程安全

C# Dispose方法的實現(xiàn)實例

  1. // Design pattern for the base class.  
  2. // By implementing IDisposable, you are announcing that instances  
  3. // of this type allocate scarce resources.  
  4. public class BaseResource: IDisposable  
  5. {  
  6. // Pointer to an external unmanaged resource.  
  7. private IntPtr handle;  
  8. // Other managed resource this class uses.  
  9. private Component Components;  
  10. // Track whether Dispose has been called.  
  11. private bool disposed = false;  
  12.  
  13. // Constructor for the BaseResource object.  
  14. public BaseResource()  
  15. {  
  16. // Insert appropriate constructor code here.  
  17. }  
  18.  
  19. // Implement IDisposable.  
  20. // Do not make this method virtual.  
  21. // A derived class should not be able to override this method.  
  22. public void Dispose()  
  23. {  
  24. Dispose(true);  
  25. // Take yourself off the Finalization queue   
  26. // to prevent finalization code for this object  
  27. // from executing a second time.  
  28. GC.SuppressFinalize(this);  
  29. }  
  30.  
  31. // Dispose(bool disposing) executes in two distinct scenarios.  
  32. // If disposing equals true, the method has been called directly  
  33. // or indirectly by a user's code. Managed and unmanaged resources  
  34. // can be disposed.  
  35. // If disposing equals false, the method has been called by the   
  36. // runtime from inside the finalizer and you should not reference   
  37. // other objects. Only unmanaged resources can be disposed.  
  38. protected virtual void Dispose(bool disposing)  
  39. {  
  40. // Check to see if Dispose has already been called.  
  41. if(!this.disposed)  
  42. {  
  43. // If disposing equals true, dispose all managed   
  44. // and unmanaged resources.  
  45. if(disposing)  
  46. {  
  47. // Dispose managed resources.  
  48. Components.Dispose();  
  49. }  
  50. // Release unmanaged resources. If disposing is false,   
  51. // only the following code is executed.  
  52. CloseHandle(handle);  
  53. handle = IntPtr.Zero;  
  54. // Note that this is not thread safe.  
  55. // Another thread could start disposing the object  
  56. // after the managed resources are disposed,  
  57. // but before the disposed flag is set to true.  
  58. // If thread safety is necessary, it must be  
  59. // implemented by the client.  
  60.  
  61. }  
  62. disposed = true;  
  63. }  
  64.  
  65. // Use C# destructor syntax for finalization code.  
  66. // This destructor will run only if the Dispose method   
  67. // does not get called.  
  68. // It gives your base class the opportunity to finalize.  
  69. // Do not provide destructors in types derived from this class.  
  70. ~BaseResource()  
  71. {  
  72. // Do not re-create Dispose clean-up code here.  
  73. // Calling Dispose(false) is optimal in terms of  
  74. // readability and maintainability.  
  75. Dispose(false);  
  76. }  
  77.  
  78. // Allow your Dispose method to be called multiple times,  
  79. // but throw an exception if the object has been disposed.  
  80. // Whenever you do something with this class,   
  81. // check to see if it has been disposed.  
  82. public void DoSomething()  
  83. {  
  84. if(this.disposed)  
  85. {  
  86. throw new ObjectDisposedException();  
  87. }  
  88. }  
  89. }  
  90.  
  91. // Design pattern for a derived class.  
  92. // Note that this derived class inherently implements the   
  93. // IDisposable interface because it is implemented in the base class.  
  94. public class MyResourceWrapper: BaseResource  
  95. {  
  96. // A managed resource that you add in this derived class.  
  97. private ManagedResource addedManaged;  
  98. // A native unmanaged resource that you add in this derived class.  
  99. private NativeResource addedNative;  
  100. private bool disposed = false;  
  101.  
  102.   // Constructor for this object.  
  103. public MyResourceWrapper()  
  104. {  
  105. // Insert appropriate constructor code here.  
  106. }  
  107.  
  108. protected override void Dispose(bool disposing)  
  109. {  
  110. if(!this.disposed)  
  111. {  
  112. try 
  113. {  
  114. if(disposing)  
  115. {  
  116. // Release the managed resources you added in  
  117. // this derived class here.  
  118. addedManaged.Dispose();  
  119. }  
  120. // Release the native unmanaged resources you added  
  121. // in this derived class here.  
  122. CloseHandle(addedNative);  
  123. this.disposed = true;  
  124. }  
  125. finally 
  126. {  
  127. // Call Dispose on your base class.  
  128. base.Dispose(disposing);  
  129. }  
  130. }  
  131. }  
  132. }  
  133.  
  134. // This derived class does not have a Finalize method  
  135. // or a Dispose method without parameters because it inherits   
  136. // them from the base class.  

C# Dispose方法的實現(xiàn)以及C# Dispose方法的一些基本內容就向你介紹到這里,希望對你了解和學習C# Dispose方法有所幫助。

【編輯推薦】

  1. C#窗體設計操作淺析
  2. C#窗體設計器開發(fā)實例詳解
  3. C#窗體移動實例解析
  4. C#透明窗體代碼詳解
  5. C#透明窗體及按鈕的效果淺析
責任編輯:仲衡 來源: MSDN
相關推薦

2009-09-07 09:53:01

C# DisposeDispose方法

2009-08-31 16:33:28

C#調用Dispose

2009-09-11 10:59:06

Effective C調用Dispose()

2011-03-29 09:14:49

Dispose模式C#

2009-08-20 16:15:19

C# 匿名方法

2009-08-10 17:36:17

C#擴展方法

2009-09-10 14:52:55

C# get

2009-09-11 09:15:06

C# get方法

2009-06-16 13:22:59

SqlConnecti

2009-08-17 16:59:47

C#轉義字符雙引號

2009-08-28 15:52:23

C#利用sharpzi

2009-08-17 17:56:32

C# 枚舉

2009-09-10 09:10:17

C# TextBox換

2009-09-02 17:24:44

C#關機代碼

2009-09-27 10:43:13

C#合并多個WORD文

2009-08-26 09:54:45

C#打印預覽C#打印

2009-08-31 16:48:02

C#實現(xiàn)IDispos

2009-09-02 15:34:37

C#實現(xiàn)插件構架

2009-09-01 18:29:24

C#實現(xiàn)多個接口

2009-08-14 16:02:50

C#啟動windows
點贊
收藏

51CTO技術棧公眾號