Skip to content

Commit 072faf6

Browse files
authored
ObjectDisposedException F# snippets (#7812)
1 parent 77bcd7b commit 072faf6

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module dispose1
2+
3+
// <Snippet1>
4+
open System
5+
open System.Threading
6+
7+
let timerNotification _ =
8+
printfn $"Timer event fired at {DateTime.Now:F}"
9+
10+
let t = new Timer(timerNotification, null, 100, Timeout.Infinite)
11+
Thread.Sleep 2000
12+
t.Dispose()
13+
14+
t.Change(200, 1000)
15+
|> ignore
16+
Thread.Sleep 3000
17+
18+
// The example displays output like the following:
19+
// Timer event fired at Monday, July 14, 2014 11:54:08 AM
20+
//
21+
// Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object.
22+
// at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period)
23+
// at <StartupCode$fs>.main()
24+
// </Snippet1>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="dispose1.fs" />
9+
<Compile Include="objdispexc.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module objdispexc
2+
3+
//<Snippet1>
4+
open System
5+
open System.IO
6+
7+
let ms = new MemoryStream 16
8+
ms.Close()
9+
try
10+
ms.ReadByte()
11+
|> ignore
12+
with :? ObjectDisposedException as e ->
13+
printfn $"Caught: {e.Message}"
14+
//</Snippet1>

xml/System/ObjectDisposedException.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
- You've called an `IDisposable` object's `Dispose` method (or an `IDisposableAsync` object's `DisposeAsync` method), and you're trying to access an instance member that gets or sets the object's state. The following example illustrates the <xref:System.ObjectDisposedException> that is thrown when you try to reset the frequency of timer notifications after you call the <xref:System.Threading.Timer.Dispose%2A?displayProperty=nameWithType> method.
6363
6464
:::code language="csharp" source="~/snippets/csharp/System/ObjectDisposedException/Overview/dispose1.cs" id="Snippet1":::
65+
:::code language="fsharp" source="~/snippets/fsharp/System/ObjectDisposedException/Overview/dispose1.fs" id="Snippet1":::
6566
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.objectdisposedexception/vb/dispose1.vb" id="Snippet1":::
6667
6768
- You've called an object's `Close` method, and you're trying to access an instance member that gets or sets the object's state. Often, the `Close` method provides a type's public implementation of the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method. The same is true for `CloseAsync` and `<xref:System.IAsyncDisposable.DisposeAsync%2A?displayProperty=nameWithType>`.
@@ -77,6 +78,7 @@
7778
7879
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/ObjDispEx/CPP/objdispexc.cpp" id="Snippet1":::
7980
:::code language="csharp" source="~/snippets/csharp/System/ObjectDisposedException/Overview/objdispexc.cs" interactive="try-dotnet" id="Snippet1":::
81+
:::code language="fsharp" source="~/snippets/fsharp/System/ObjectDisposedException/Overview/objdispexc.fs" id="Snippet1":::
8082
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/ObjDispEx/VB/objdispexc.vb" id="Snippet1":::
8183
8284
This code produces the following output:

0 commit comments

Comments
 (0)