Skip to content

System.ObjectDisposedException F# snippets #7812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module dispose1

// <Snippet1>
open System
open System.Threading

let timerNotification _ =
printfn $"Timer event fired at {DateTime.Now:F}"

let t = new Timer(timerNotification, null, 100, Timeout.Infinite)
Thread.Sleep 2000
t.Dispose()

t.Change(200, 1000)
|> ignore
Thread.Sleep 3000

// 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 <StartupCode$fs>.main()
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="dispose1.fs" />
<Compile Include="objdispexc.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module objdispexc

//<Snippet1>
open System
open System.IO

let ms = new MemoryStream 16
ms.Close()
try
ms.ReadByte()
|> ignore
with :? ObjectDisposedException as e ->
printfn $"Caught: {e.Message}"
//</Snippet1>
2 changes: 2 additions & 0 deletions xml/System/ObjectDisposedException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- 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.

:::code language="csharp" source="~/snippets/csharp/System/ObjectDisposedException/Overview/dispose1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/ObjectDisposedException/Overview/dispose1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.objectdisposedexception/vb/dispose1.vb" id="Snippet1":::

- 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>`.
Expand All @@ -77,6 +78,7 @@

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/ObjDispEx/CPP/objdispexc.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System/ObjectDisposedException/Overview/objdispexc.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/ObjectDisposedException/Overview/objdispexc.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/ObjDispEx/VB/objdispexc.vb" id="Snippet1":::

This code produces the following output:
Expand Down