문제

나가라는 클래스 DatabaseHelper 래핑 표시됩니다.What's 는 적절한 방법을 설치에 대한 자세한 내용은 이 클래스는 사용하는 문장인가요?를 구현하고 있습니다 IDisposible 지만,나는 확실하지 않다면 어디서 나는 전화 연결합니다.Close()또는 연결합니다.폐기().

때 나는 단지 전화를 연결합니다.폐기()내에서 자신의 처분()메소드,나는 때때로 얻을 SocketException 에 대한 귀하의 의견을 이해하기 위해 개체입니다.나는 가정이기 때문에 이전 연결되는 열려 있지만,없는 상세정보를 첨부하여 예외가 없습니다 확실히 알 수 있습니다.

도움이 되었습니까?

해결책

전화를 연결합니다.폐기()내에서 당신의 처분 방법입니다.을 봐야 하는 표준을 구현하는 패턴 IDisposable 는 위성 프로그램을 시청하실 수 있으며 단순히 구현 IDisposable 인터페이스 할 수 있습 폐기 위해 관리되지 않는 물체 등의:

public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            // Dispose managed resources.
        }

        // There are no unmanaged resources to release, but
        // if we add them, they need to be released here.
    }
    disposed = true;

    // If it is available, make the call to the
    // base class's Dispose(Boolean) method
    base.Dispose(disposing);
}

(에서 가져온 http://msdn.microsoft.com/en-us/library/system.idisposable.aspx).

다른 팁

에 따르면 이것 뉴스 그룹 :

다음은 idbConnection.dispose ()가 구현되는 방법입니다 (반사기 유틸리티 표시) :

sqlclient :

protected override void Dispose(bool disposing)
{
       if (disposing)
       {
             switch (this._objectState)
             {
                   case ConnectionState.Open:
                   {
                         this.Close();
                         break;
                   }
             }
             this._constr = null;
       }
       base.Dispose(disposing);
}

Odbc:
protected override void Dispose(bool disposing)
{
       if (disposing)
       {
             this._constr = null;
             this.Close();
             CNativeBuffer buffer1 = this._buffer;
             if (buffer1 != null)
             {
                   buffer1.Dispose();
                   this._buffer = null;
             }
       }
       base.Dispose(disposing);
}

OleDb:
protected override void Dispose(bool disposing)
{
       if (disposing)
       {
             if (this.objectState != 0)
             {
                   this.DisposeManaged();
                   if (base.DesignMode)
                   {
                         OleDbConnection.ReleaseObjectPool();
                   }
                   this.OnStateChange(ConnectionState.Open, ConnectionState.Closed);
             }
             if (this.propertyIDSet != null)
             {
                   this.propertyIDSet.Dispose();
                   this.propertyIDSet = null;
             }
             this._constr = null;
       }
       base.Dispose(disposing);
}

처분 방법은 연결이 열려있는 경우에만 연결을 닫으려고 시도해야합니다.

이 파괴자 구문은 실제로 최종화기입니다. Finalizer는 Dispose (False) 메소드를 호출합니다.

    #region IDisposable Members
    private bool _isDisposed;

    private void ThrowIfDisposed()
    {
        if (_isDisposed)
            throw new ObjectDisposedException(this.GetType().Name);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_isDisposed)
        {
            if (disposing)
            {
                //part 1 : disposing managed objects
                _command.Dispose();
                _command.Connection.Dispose();
                if (_command.Transaction != null)
                    _command.Transaction.Dispose();
            }
            //part 2: disposing unmanged objects. Here there are no unmanged objects.
            _isDisposed = true;
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    //~DbCommandExecutor() //No need of finalize here. Because there is no unmanged objects in my class. ie, no code in part 2.
    //{
    //    Dispose(false);
    //}
    #endregion

거기 있습니다 필요 없음 Finalizer (또는 Destructor) 구문 코드에 Part 2 코드가있을 때까지. 그렇지 않으면 안전한 쪽을 위해 구현해야합니다. 즉, 프로그래머가 처분 방법을 올바르게 호출하지 않더라도 마무리는 관리되지 않는 자원을 정리해야합니다.

MSDN에서 예제를 비교하십시오

http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx & http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

Idisposable 구현 패턴을 완료하기 위해 Dispose () 메소드를 호출하는 클래스에 대한 Finalizer (Destructor)를 포함하는 규칙입니다. 이것은 실패 메커니즘으로 작용하여 클래스의 소비자가 dispose ()를 호출하지 못하면 관리되지 않는 물체를 폐기 할 수 있습니다.

    ~MyClass() 
    {
        Dispose(false);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top