File tree Expand file tree Collapse file tree 6 files changed +85
-0
lines changed
snippets/fsharp/System/EventArgs/Overview Expand file tree Collapse file tree 6 files changed +85
-0
lines changed Original file line number Diff line number Diff line change 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 =" programwithdata.fs" />
9+ <Compile Include =" programnodata.fs" />
10+ </ItemGroup >
11+ </Project >
Original file line number Diff line number Diff line change 1+ module programnodata
2+
3+ // <snippet5>
4+ open System
5+
6+ type Counter ( threshold ) =
7+ let mutable total = 0
8+
9+ let thresholdReached = Event<_>()
10+
11+ member this.Add ( x ) =
12+ total <- total + x
13+ if total >= threshold then
14+ thresholdReached.Trigger( this, EventArgs.Empty)
15+
16+ [<CLIEvent>]
17+ member _.ThresholdReached = thresholdReached.Publish
18+
19+ let c_ThresholdReached ( sender , arg ) =
20+ printfn " The threshold was reached."
21+ exit 0
22+
23+ let c = Counter( Random() .Next 10 )
24+ c.ThresholdReached.Add c_ ThresholdReached
25+
26+ printfn " press 'a' key to increase total"
27+ while Console.ReadKey( true ) .KeyChar = 'a' do
28+ printfn " adding one"
29+ c.Add 1
30+
31+ // </snippet5>
Original file line number Diff line number Diff line change 1+ module programwithdata
2+
3+ // <snippet6>
4+ open System
5+
6+ type ThresholdReachedEventArgs ( threshold , timeReached ) =
7+ inherit EventArgs()
8+ member _.Threshold = threshold
9+ member _.TimeReached = timeReached
10+
11+ type Counter ( threshold ) =
12+ let mutable total = 0
13+
14+ let thresholdReached = Event<_>()
15+
16+ member this.Add ( x ) =
17+ total <- total + x
18+ if total >= threshold then
19+ let args = ThresholdReachedEventArgs( threshold, DateTime.Now)
20+ thresholdReached.Trigger( this, args)
21+
22+ [<CLIEvent>]
23+ member _.ThresholdReached = thresholdReached.Publish
24+
25+ let c_ThresholdReached ( sender , e : ThresholdReachedEventArgs ) =
26+ printfn $" The threshold of {e.Threshold} was reached at {e.TimeReached}."
27+ exit 0
28+
29+ let c = Counter( Random() .Next 10 )
30+ c.ThresholdReached.Add c_ ThresholdReached
31+
32+ printfn " press 'a' key to increase total"
33+ while Console.ReadKey( true ) .KeyChar = 'a' do
34+ printfn " adding one"
35+ c.Add 1
36+ // </snippet6>
Original file line number Diff line number Diff line change 6868
6969 :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/eventsoverview/cpp/programwithdata.cpp" id="Snippet6":::
7070 :::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programwithdata.cs" id="Snippet6":::
71+ :::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs" id="Snippet6":::
7172 :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb" id="Snippet6":::
7273
7374 ]]> </format >
177178 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.
178179
179180 :::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programnodata.cs" id="Snippet5":::
181+ :::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programnodata.fs" id="Snippet5":::
180182 :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1nodata.vb" id="Snippet5":::
181183
182184 ]]> </format >
185187 <related type =" Article" href =" /dotnet/standard/events/how-to-raise-and-consume-events" >How to: Raise and Consume Events</related >
186188 <related type =" Article" href =" /dotnet/visual-basic/programming-guide/language-features/events/" >Events (Visual Basic)</related >
187189 <related type =" Article" href =" /dotnet/csharp/programming-guide/events/" >Events (C# Programming Guide)</related >
190+ <related type =" Article" href =" /dotnet/fsharp/language-reference/members/events/" >Events (F#)</related >
188191 <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 >
189192 </Docs >
190193 </Member >
Original file line number Diff line number Diff line change 8787
8888 :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/eventsoverview/cpp/programwithdata.cpp" id="Snippet6":::
8989 :::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programwithdata.cs" id="Snippet6":::
90+ :::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs" id="Snippet6":::
9091 :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb" id="Snippet6":::
9192
9293 ]]> </format >
9899 <related type =" Article" href =" /dotnet/standard/events/how-to-raise-and-consume-events" >How to: Raise and Consume Events</related >
99100 <related type =" Article" href =" /dotnet/visual-basic/programming-guide/language-features/events/" >Events (Visual Basic)</related >
100101 <related type =" Article" href =" /dotnet/csharp/programming-guide/events/" >Events (C# Programming Guide)</related >
102+ <related type =" Article" href =" /dotnet/fsharp/language-reference/members/events/" >Events (F#)</related >
101103 <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 >
102104 </Docs >
103105</Type >
Original file line number Diff line number Diff line change 9090
9191 :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/eventsoverview/cpp/programwithdata.cpp" id="Snippet6":::
9292 :::code language="csharp" source="~/snippets/csharp/System/EventArgs/Overview/programwithdata.cs" id="Snippet6":::
93+ :::code language="fsharp" source="~/snippets/fsharp/System/EventArgs/Overview/programwithdata.fs" id="Snippet6":::
9394 :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb" id="Snippet6":::
9495
9596 ]]> </format >
102103 <related type =" Article" href =" /dotnet/standard/events/how-to-raise-and-consume-events" >How to: Raise and Consume Events</related >
103104 <related type =" Article" href =" /dotnet/visual-basic/programming-guide/language-features/events/" >Events (Visual Basic)</related >
104105 <related type =" Article" href =" /dotnet/csharp/programming-guide/events/" >Events (C# Programming Guide)</related >
106+ <related type =" Article" href =" /dotnet/fsharp/language-reference/members/events/" >Events (F#)</related >
105107 <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 >
106108 </Docs >
107109</Type >
You can’t perform that action at this time.
0 commit comments