From 714bdd3f9e92b41c822637222d680d1f66e1b94b Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Wed, 9 Feb 2022 21:02:12 -0800
Subject: [PATCH] EventArgs F# snippets
---
.../System/EventArgs/Overview/fs.fsproj | 11 ++++++
.../EventArgs/Overview/programnodata.fs | 31 ++++++++++++++++
.../EventArgs/Overview/programwithdata.fs | 36 +++++++++++++++++++
xml/System/EventArgs.xml | 3 ++
xml/System/EventHandler.xml | 2 ++
xml/System/EventHandler`1.xml | 2 ++
6 files changed, 85 insertions(+)
create mode 100644 snippets/fsharp/System/EventArgs/Overview/fs.fsproj
create mode 100644 snippets/fsharp/System/EventArgs/Overview/programnodata.fs
create mode 100644 snippets/fsharp/System/EventArgs/Overview/programwithdata.fs
diff --git a/snippets/fsharp/System/EventArgs/Overview/fs.fsproj b/snippets/fsharp/System/EventArgs/Overview/fs.fsproj
new file mode 100644
index 00000000000..a9049d2afbc
--- /dev/null
+++ b/snippets/fsharp/System/EventArgs/Overview/fs.fsproj
@@ -0,0 +1,11 @@
+
+
+ Exe
+ net6.0
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/snippets/fsharp/System/EventArgs/Overview/programnodata.fs b/snippets/fsharp/System/EventArgs/Overview/programnodata.fs
new file mode 100644
index 00000000000..9eaa107c106
--- /dev/null
+++ b/snippets/fsharp/System/EventArgs/Overview/programnodata.fs
@@ -0,0 +1,31 @@
+module programnodata
+
+//
+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)
+
+ []
+ 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
+
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs b/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs
new file mode 100644
index 00000000000..fc81a246ed7
--- /dev/null
+++ b/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs
@@ -0,0 +1,36 @@
+module programwithdata
+
+//
+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)
+
+ []
+ 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
+//
\ No newline at end of file
diff --git a/xml/System/EventArgs.xml b/xml/System/EventArgs.xml
index 335b6a772f2..bb3cd1b1687 100644
--- a/xml/System/EventArgs.xml
+++ b/xml/System/EventArgs.xml
@@ -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":::
]]>
@@ -177,6 +178,7 @@
The following example shows a simple counting application that raises an event when a threshold is equaled or exceeded. The 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":::
]]>
@@ -185,6 +187,7 @@
How to: Raise and Consume Events
Events (Visual Basic)
Events (C# Programming Guide)
+ Events (F#)
Events and routed events overview (Windows store apps)
diff --git a/xml/System/EventHandler.xml b/xml/System/EventHandler.xml
index 1733fe46594..e0576390ee7 100644
--- a/xml/System/EventHandler.xml
+++ b/xml/System/EventHandler.xml
@@ -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":::
]]>
@@ -98,6 +99,7 @@
How to: Raise and Consume Events
Events (Visual Basic)
Events (C# Programming Guide)
+ Events (F#)
Events and routed events overview (Windows store apps)
diff --git a/xml/System/EventHandler`1.xml b/xml/System/EventHandler`1.xml
index df881bf3355..8fbab252631 100644
--- a/xml/System/EventHandler`1.xml
+++ b/xml/System/EventHandler`1.xml
@@ -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":::
]]>
@@ -102,6 +103,7 @@
How to: Raise and Consume Events
Events (Visual Basic)
Events (C# Programming Guide)
+ Events (F#)
Events and routed events overview (Windows store apps)