Skip to content
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,80 @@
// <Snippet4>
// The following example demonstrates using asynchronous methods to
// get Domain Name System information for the specified host computers.
// This example uses a delegate to obtain the results of each asynchronous
// operation.

open System
open System.Net
open System.Net.Sockets
open System.Threading
open System.Collections.Specialized

let mutable requestCounter = 0
let hostData = ResizeArray<Result<IPHostEntry, string>>()
let hostNames = StringCollection()

let updateUserInterface () =
// Print a message to indicate that the application
// is still working on the remaining requests.
printfn $"{requestCounter} requests remaining."

// The following function is called when each asynchronous operation completes.
let processDnsInformation (result: IAsyncResult) =
string result.AsyncState
|> hostNames.Add
|> ignore
try
try
// Get the results.
Dns.EndGetHostEntry result
|> Ok
|> hostData.Add
// Store the exception message.
with :? SocketException as e ->
Error e.Message
|> hostData.Add
finally
// Decrement the request counter in a thread-safe manner.
Interlocked.Decrement &requestCounter
|> ignore

// Create the delegate that will process the results of the asynchronous request.
let callBack = AsyncCallback processDnsInformation

let mutable host = " "
while host.Length > 0 do
printf " Enter the name of a host computer or <enter> to finish: "
host <- stdin.ReadLine()
if host.Length > 0 then
// Increment the request counter in a thread safe manner.
Interlocked.Increment &requestCounter |> ignore
// Start the asynchronous request for DNS information.
Dns.BeginGetHostEntry(host, callBack, host) |> ignore

// The user has entered all of the host names for lookup.
// Now wait until the threads complete.
while requestCounter > 0 do
updateUserInterface ()

// Display the results.
for i = 0 to hostNames.Count - 1 do
match hostData[i] with
| Error message ->
// A SocketException was thrown.
printfn $"Request for {hostNames[i]} returned message: {message}"
| Ok entry ->
// Get the results.
let aliases = entry.Aliases
let addresses = entry.AddressList
if aliases.Length > 0 then
printfn $"Aliases for {hostNames[i]}"
for alias in aliases do
printfn $"{alias}"

if addresses.Length > 0 then
printfn $"Addresses for {hostNames[i]}"
for address in addresses do
printfn $"{address}"

//</Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="AsyncDelegateNoStateObject.fs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions xml/System/AsyncCallback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
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.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/AsyncDesignPattern/CS/AsyncDelegateNoStateObject.cs" id="Snippet4":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/AsyncDelegateNoStateObject.fs" id="Snippet4":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/AsyncDesignPattern/VB/AsyncDelegateNoState.vb" id="Snippet4":::

]]></format>
Expand Down