diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/AsyncDelegateNoStateObject.fs b/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/AsyncDelegateNoStateObject.fs new file mode 100644 index 00000000000..6890f2029a2 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/AsyncDelegateNoStateObject.fs @@ -0,0 +1,80 @@ +// +// 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>() +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 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}" + +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/fs.fsproj new file mode 100644 index 00000000000..0094301d0d9 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/AsyncDesignPattern/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/xml/System/AsyncCallback.xml b/xml/System/AsyncCallback.xml index 1c8ebc191e2..f431224f9a0 100644 --- a/xml/System/AsyncCallback.xml +++ b/xml/System/AsyncCallback.xml @@ -71,6 +71,7 @@ The following code example demonstrates using asynchronous methods in the class to retrieve Domain Name System (DNS) information for user-specified computers. This example creates an 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"::: ]]>