首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

用 C# 編寫網路電話 (4):SoundPlayer 屬性與 IDisposable 資源釋放

     編輯:關於C語言

公開屬性

#region 公開屬性
public int BitsPerSample => m_Buffer.Format.BitsPerSample;
public int Channels => m_Buffer.Format.Channels;
public Device Device => m_Device;
public int SamplingRate => m_Buffer.Format.SamplesPerSecond;
#endregion

IDisposable 實作與解構子

#region IDisposable Members
public void Dispose()
{
    Stop();
    if (m_Buffer != null) { m_Buffer.Dispose(); m_Buffer = null; }
    if (m_OwnsDevice && m_Device != null) { m_Device.Dispose(); m_Device = null; }
    GC.SuppressFinalize(this);
}

~SoundPlayer() => Dispose();
#endregion

設計重點

  • 屬性直接透過 SecondaryBuffer.Format 暴露音訊格式資訊。
  • Dispose() 確保緩衝區與裝置 (若為自建) 正確釋放,抑制 Finalize 減少 GC 壓力。
  • 解構子作為安全網呼叫 Dispose()。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved