本文整理汇总了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()