Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.EventArgs F# snippets #7710

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
merged 1 commit into from
Feb 14, 2022
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
11 changes: 11 additions & 0 deletions snippets/fsharp/System/EventArgs/Overview/fs.fsproj
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="programwithdata.fs" />
<Compile Include="programnodata.fs" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions snippets/fsharp/System/EventArgs/Overview/programnodata.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module programnodata

// <snippet5>
open System

type Counter(threshold) =
let mutable total = 0

let thresholdReached = Event<_>()

member this.Add(x) =
total <- total + x
if total >= threshold then
thresholdReached.Trigger(this, EventArgs.Empty)

[<CLIEvent>]
member _.ThresholdReached = thresholdReached.Publish

let c_ThresholdReached(sender, arg) =
printfn "The threshold was reached."
exit 0

let c = Counter(Random().Next 10)
c.ThresholdReached.Add c_ThresholdReached

printfn "press 'a' key to increase total"
while Console.ReadKey(true).KeyChar = 'a' do
printfn "adding one"
c.Add 1

// </snippet5>
36 changes: 36 additions & 0 deletions snippets/fsharp/System/EventArgs/Overview/programwithdata.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module programwithdata

// <snippet6>
open System

type ThresholdReachedEventArgs(threshold, timeReached) =
inherit EventArgs()
member _.Threshold = threshold
member _.TimeReached = timeReached

type Counter(threshold) =
let mutable total = 0

let thresholdReached = Event<_>()

member this.Add(x) =
total <- total + x
if total >= threshold then
let args = ThresholdReachedEventArgs(threshold, DateTime.Now)
thresholdReached.Trigger(this, args)

[<CLIEvent>]
member _.ThresholdReached = thresholdReached.Publish

let c_ThresholdReached(sender, e: ThresholdReachedEventArgs) =
printfn $"The threshold of {e.Threshold} was reached at {e.TimeReached}."
exit 0

let c = Counter(Random().Next 10)
c.ThresholdReached.Add c_ThresholdReached

printfn "press 'a' key to increase total"
while Console.ReadKey(true).KeyChar = 'a' do
printfn "adding one"
c.Add 1
// </snippet6>
3 changes: 3 additions & 0 deletions xml/System/EventArgs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/eventsoverview/cpp/programwithdata.cpp" id="Snippet6":::
:::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programwithdata.cs" id="Snippet6":::
:::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs" id="Snippet6":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb" id="Snippet6":::

]]></format>
Expand Down Expand Up @@ -177,6 +178,7 @@
The following example shows a simple counting application that raises an event when a threshold is equaled or exceeded. The <xref:System.EventArgs.Empty> field is passed to the `OnThresholdReached` method.

:::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programnodata.cs" id="Snippet5":::
:::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programnodata.fs" id="Snippet5":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1nodata.vb" id="Snippet5":::

]]></format>
Expand All @@ -185,6 +187,7 @@
<related type="Article" href="/dotnet/standard/events/how-to-raise-and-consume-events">How to: Raise and Consume Events</related>
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/events/">Events (Visual Basic)</related>
<related type="Article" href="/dotnet/csharp/programming-guide/events/">Events (C# Programming Guide)</related>
<related type="Article" href="/dotnet/fsharp/language-reference/members/events/">Events (F#)</related>
<related type="Article" href="https://docs.microsoft.com/previous-versions/windows/apps/hh758286(v=win.10)">Events and routed events overview (Windows store apps)</related>
</Docs>
</Member>
Expand Down
2 changes: 2 additions & 0 deletions xml/System/EventHandler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/eventsoverview/cpp/programwithdata.cpp" id="Snippet6":::
:::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programwithdata.cs" id="Snippet6":::
:::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs" id="Snippet6":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb" id="Snippet6":::

]]></format>
Expand All @@ -98,6 +99,7 @@
<related type="Article" href="/dotnet/standard/events/how-to-raise-and-consume-events">How to: Raise and Consume Events</related>
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/events/">Events (Visual Basic)</related>
<related type="Article" href="/dotnet/csharp/programming-guide/events/">Events (C# Programming Guide)</related>
<related type="Article" href="/dotnet/fsharp/language-reference/members/events/">Events (F#)</related>
<related type="Article" href="https://docs.microsoft.com/previous-versions/windows/apps/hh758286(v=win.10)">Events and routed events overview (Windows store apps)</related>
</Docs>
</Type>
2 changes: 2 additions & 0 deletions xml/System/EventHandler`1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/eventsoverview/cpp/programwithdata.cpp" id="Snippet6":::
:::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programwithdata.cs" id="Snippet6":::
:::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs" id="Snippet6":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb" id="Snippet6":::

]]></format>
Expand All @@ -102,6 +103,7 @@
<related type="Article" href="/dotnet/standard/events/how-to-raise-and-consume-events">How to: Raise and Consume Events</related>
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/events/">Events (Visual Basic)</related>
<related type="Article" href="/dotnet/csharp/programming-guide/events/">Events (C# Programming Guide)</related>
<related type="Article" href="/dotnet/fsharp/language-reference/members/events/">Events (F#)</related>
<related type="Article" href="https://docs.microsoft.com/previous-versions/windows/apps/hh758286(v=win.10)">Events and routed events overview (Windows store apps)</related>
</Docs>
</Type>