Skip to content

Commit 4e1212f

Browse files
authored
System.AsyncCallback F# snippet (#7518)
1 parent 1568dfc commit 4e1212f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// <Snippet4>
2+
// The following example demonstrates using asynchronous methods to
3+
// get Domain Name System information for the specified host computers.
4+
// This example uses a delegate to obtain the results of each asynchronous
5+
// operation.
6+
7+
open System
8+
open System.Net
9+
open System.Net.Sockets
10+
open System.Threading
11+
open System.Collections.Specialized
12+
13+
let mutable requestCounter = 0
14+
let hostData = ResizeArray<Result<IPHostEntry, string>>()
15+
let hostNames = StringCollection()
16+
17+
let updateUserInterface () =
18+
// Print a message to indicate that the application
19+
// is still working on the remaining requests.
20+
printfn $"{requestCounter} requests remaining."
21+
22+
// The following function is called when each asynchronous operation completes.
23+
let processDnsInformation (result: IAsyncResult) =
24+
string result.AsyncState
25+
|> hostNames.Add
26+
|> ignore
27+
try
28+
try
29+
// Get the results.
30+
Dns.EndGetHostEntry result
31+
|> Ok
32+
|> hostData.Add
33+
// Store the exception message.
34+
with :? SocketException as e ->
35+
Error e.Message
36+
|> hostData.Add
37+
finally
38+
// Decrement the request counter in a thread-safe manner.
39+
Interlocked.Decrement &requestCounter
40+
|> ignore
41+
42+
// Create the delegate that will process the results of the asynchronous request.
43+
let callBack = AsyncCallback processDnsInformation
44+
45+
let mutable host = " "
46+
while host.Length > 0 do
47+
printf " Enter the name of a host computer or <enter> to finish: "
48+
host <- stdin.ReadLine()
49+
if host.Length > 0 then
50+
// Increment the request counter in a thread safe manner.
51+
Interlocked.Increment &requestCounter |> ignore
52+
// Start the asynchronous request for DNS information.
53+
Dns.BeginGetHostEntry(host, callBack, host) |> ignore
54+
55+
// The user has entered all of the host names for lookup.
56+
// Now wait until the threads complete.
57+
while requestCounter > 0 do
58+
updateUserInterface ()
59+
60+
// Display the results.
61+
for i = 0 to hostNames.Count - 1 do
62+
match hostData[i] with
63+
| Error message ->
64+
// A SocketException was thrown.
65+
printfn $"Request for {hostNames[i]} returned message: {message}"
66+
| Ok entry ->
67+
// Get the results.
68+
let aliases = entry.Aliases
69+
let addresses = entry.AddressList
70+
if aliases.Length > 0 then
71+
printfn $"Aliases for {hostNames[i]}"
72+
for alias in aliases do
73+
printfn $"{alias}"
74+
75+
if addresses.Length > 0 then
76+
printfn $"Addresses for {hostNames[i]}"
77+
for address in addresses do
78+
printfn $"{address}"
79+
80+
//</Snippet4>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="AsyncDelegateNoStateObject.fs" />
8+
</ItemGroup>
9+
</Project>

xml/System/AsyncCallback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
The following code example demonstrates using asynchronous methods in the <xref:System.Net.Dns> class to retrieve Domain Name System (DNS) information for user-specified computers. This example creates an <xref:System.AsyncCallback> delegate that references the `ProcessDnsInformation` method. This method is called once for each asynchronous request for DNS information.
7272
7373
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/AsyncDesignPattern/CS/AsyncDelegateNoStateObject.cs" id="Snippet4":::
74+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/AsyncDelegateNoStateObject.fs" id="Snippet4":::
7475
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/AsyncDesignPattern/VB/AsyncDelegateNoState.vb" id="Snippet4":::
7576
7677
]]></format>

0 commit comments

Comments
 (0)