ObjectDisposedException Třída
Definice
Výjimka, která je vyvolána, když je operace provedena u uvolněného objektu.The exception that is thrown when an operation is performed on a disposed object.
public ref class ObjectDisposedException : InvalidOperationException
public class ObjectDisposedException : InvalidOperationException
[System.Serializable]
public class ObjectDisposedException : InvalidOperationException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ObjectDisposedException : InvalidOperationException
type ObjectDisposedException = class
inherit InvalidOperationException
[<System.Serializable>]
type ObjectDisposedException = class
inherit InvalidOperationException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ObjectDisposedException = class
inherit InvalidOperationException
Public Class ObjectDisposedException
Inherits InvalidOperationException
- Dědičnost
- Dědičnost
- Atributy
Příklady
Následující příklad ukazuje chybu, která způsobuje ObjectDisposedException
vyvolání výjimky.The following example demonstrates an error that causes the ObjectDisposedException
exception to be thrown.
using namespace System;
using namespace System::IO;
int main()
{
MemoryStream^ ms = gcnew MemoryStream( 16 );
ms->Close();
try
{
ms->ReadByte();
}
catch ( ObjectDisposedException^ e )
{
Console::WriteLine( "Caught: {0}", e->Message );
}
}
using System;
using System.IO;
public class ObjectDisposedExceptionTest
{
public static void Main()
{
MemoryStream ms = new MemoryStream(16);
ms.Close();
try
{
ms.ReadByte();
}
catch (ObjectDisposedException e)
{
Console.WriteLine("Caught: {0}", e.Message);
}
}
}
Imports System.IO
Public Class ObjectDisposedExceptionTest
Public Shared Sub Main()
Dim ms As New MemoryStream(16)
ms.Close()
Try
ms.ReadByte()
Catch e As ObjectDisposedException
Console.WriteLine("Caught: {0}", e.Message)
End Try
End Sub
End Class
Výsledkem tohoto kódu je následující výstup:This code produces the following output:
Caught:
Cannot access a closed Stream.
Poznámky
ObjectDisposedExceptionVýjimka je vyvolána, když se pokusíte o přístup k členu objektu, který implementuje IDisposable rozhraní a tento objekt byl vyřazen.An ObjectDisposedException is thrown when you try to access a member of an object that implements the IDisposable interface, and that object has been disposed. Tato výjimka je obvykle způsobena jednou z následujících podmínek:Typically, this exception is caused by one of the following conditions:
Volali jste
Dispose
metodu objektu a snažíte se získat přístup ke členu instance, který získá nebo nastaví stav objektu.You've called an object'sDispose
method, and you're trying to access an instance member that gets or sets the object's state. Následující příklad ukazuje ObjectDisposedException , který je vyvolána při pokusu o resetování frekvence oznámení časovače po volání Timer.Dispose metody.The following example illustrates the ObjectDisposedException that is thrown when you try to reset the frequency of timer notifications after you call the Timer.Dispose method.using System; using System.Threading; public class Example { public static void Main() { Timer t = new Timer(TimerNotification, null, 100, Timeout.Infinite); Thread.Sleep(2000); t.Dispose(); t.Change(200, 1000); Thread.Sleep(3000); } private static void TimerNotification(Object obj) { Console.WriteLine("Timer event fired at {0:F}", DateTime.Now); } } // The example displays output like the following: // Timer event fired at Monday, July 14, 2014 11:54:08 AM // // Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object. // at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period) // at Example.Main()
Imports System.Threading Module Example Public Sub Main() Dim t As New Timer(AddressOf TimerNotification, Nothing, 100, Timeout.Infinite) Thread.Sleep(2000) t.Dispose() t.Change(200, 1000) Thread.Sleep(3000) End Sub Private Sub TimerNotification(obj As Object) Console.WriteLine("Timer event fired at {0:F}", Date.Now) End Sub End Module ' The example displays output like the following: ' Timer event fired at Monday, July 14, 2014 11:54:08 AM ' ' Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object. ' at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period) ' at Example.Main()
Volali jste
Close
metodu objektu a snažíte se získat přístup ke členu instance, který získá nebo nastaví stav objektu.You've called an object'sClose
method, and you're trying to access an instance member that gets or sets the object's state.Close
Metoda často poskytuje veřejnou implementaci IDisposable.Dispose metody typu.Often, theClose
method provides a type's public implementation of the IDisposable.Dispose method.Volali jste
Dispose
metodu objektu víckrát.You've called an object'sDispose
method multiple times. Obvykle to nevyvolává výjimku.Typically, this doesn't throw an exception. V závislosti na tom, jak typ implementuje IDisposable.Dispose , však nemusí umožňovat více voláníDispose
.However, depending on how a type implements IDisposable.Dispose, it may not allow multiple calls toDispose
.
Ve většině případů tato výjimka vyplývá z chyby vývojáře.In most cases, this exception results from developer error. Místo zpracování chyby v try
/ catch
bloku byste měli chybu opravit, obvykle změnou instance objektu.Instead of handling the error in a try
/catch
block, you should correct the error, typically by reinstantiating the object.
Konstruktory
ObjectDisposedException(SerializationInfo, StreamingContext) |
Inicializuje novou instanci třídy ObjectDisposedException se serializovanými daty.Initializes a new instance of the ObjectDisposedException class with serialized data. |
ObjectDisposedException(String) |
Inicializuje novou instanci ObjectDisposedException třídy s řetězcem, který obsahuje název vyřazeného objektu.Initializes a new instance of the ObjectDisposedException class with a string containing the name of the disposed object. |
ObjectDisposedException(String, Exception) |
Inicializuje novou instanci ObjectDisposedException třídy se zadanou chybovou zprávou a odkazem na vnitřní výjimku, která je příčinou této výjimky.Initializes a new instance of the ObjectDisposedException class with a specified error message and a reference to the inner exception that is the cause of this exception. |
ObjectDisposedException(String, String) |
Inicializuje novou instanci ObjectDisposedException třídy se zadaným názvem objektu a zprávou.Initializes a new instance of the ObjectDisposedException class with the specified object name and message. |
Vlastnosti
Data |
Získá kolekci párů klíč/hodnota, které poskytují další uživatelsky definované informace o výjimce.Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Zděděno od Exception) |
HelpLink |
Získá nebo nastaví odkaz na soubor s nápovědě spojený s touto výjimkou.Gets or sets a link to the help file associated with this exception. (Zděděno od Exception) |
HResult |
Získá nebo nastaví hodnotu HRESULT, kódované číselné hodnoty, která je přiřazena konkrétní výjimce.Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Zděděno od Exception) |
InnerException |
Získá Exception instanci, která způsobila aktuální výjimku.Gets the Exception instance that caused the current exception. (Zděděno od Exception) |
Message |
Získá zprávu popisující chybu.Gets the message that describes the error. |
ObjectName |
Získá název vyřazeného objektu.Gets the name of the disposed object. |
Source |
Získá nebo nastaví název aplikace nebo objektu, který způsobuje chybu.Gets or sets the name of the application or the object that causes the error. (Zděděno od Exception) |
StackTrace |
Načte řetězcovou reprezentaci okamžitých snímků v zásobníku volání.Gets a string representation of the immediate frames on the call stack. (Zděděno od Exception) |
TargetSite |
Získá metodu, která vyvolá aktuální výjimku.Gets the method that throws the current exception. (Zděděno od Exception) |
Metody
Equals(Object) |
Určí, zda se zadaný objekt rovná aktuálnímu objektu.Determines whether the specified object is equal to the current object. (Zděděno od Object) |
GetBaseException() |
Při přepsání v odvozené třídě vrátí hodnotu Exception , která je hlavní příčinou jedné nebo více následných výjimek.When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Zděděno od Exception) |
GetHashCode() |
Slouží jako výchozí funkce hash.Serves as the default hash function. (Zděděno od Object) |
GetObjectData(SerializationInfo, StreamingContext) |
Načte SerializationInfo objekt s názvem parametru a dalšími informacemi o výjimce.Retrieves the SerializationInfo object with the parameter name and additional exception information. |
GetObjectData(SerializationInfo, StreamingContext) |
Při přepsání v odvozené třídě nastaví na SerializationInfo výjimku informace o výjimce.When overridden in a derived class, sets the SerializationInfo with information about the exception. (Zděděno od Exception) |
GetType() |
Získá typ modulu runtime aktuální instance.Gets the runtime type of the current instance. (Zděděno od Exception) |
MemberwiseClone() |
Vytvoří kopii aktuálního seznamu Object .Creates a shallow copy of the current Object. (Zděděno od Object) |
ToString() |
Vytvoří a vrátí řetězcovou reprezentaci aktuální výjimky.Creates and returns a string representation of the current exception. (Zděděno od Exception) |
Události
SerializeObjectState |
Nastane, pokud je serializovaná výjimka pro vytvoření objektu stavu výjimky, který obsahuje Serializovaná data o výjimce.Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Zděděno od Exception) |