本文整理匯總了C#中System.ObjectDisposedException類的典型用法代碼示例。如果您正苦於以下問題:C# ObjectDisposedException類的具體用法?C# ObjectDisposedException怎麽用?C# ObjectDisposedException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ObjectDisposedException類屬於System命名空間,在下文中一共展示了ObjectDisposedException類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
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);
}
}
}
示例2: Main
//引入命名空間
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);
}
}
輸出:
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()