Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
Azokat az alkalmazásokat, amelyek nem tudják folytatni a munkát, miközben az aszinkron művelet eredményeire várnak, a művelet befejezéséig le kell tiltani. Az alábbi lehetőségek egyikével letilthatja az alkalmazás fő szálát, miközben az aszinkron művelet befejezésére vár:
Hívja meg az aszinkron műveleteket EndOperationName metódust. Ezt a megközelítést ebben a témakörben mutatjuk be.
Az aszinkron művelet AsyncWaitHandleIAsyncResult metódus által visszaadott tulajdonságát, a -t használja. Ezt a megközelítést bemutató példát Az alkalmazásvégrehajtás blokkolása AsyncWaitHandlehasználatával című témakörben talál.
Az EndOperationName metódust az aszinkron művelet befejezéséig letiltó alkalmazások általában meghívják a BeginOperationName metódust, végrehajtanak minden olyan munkát, amely a művelet eredménye nélkül is elvégezhető, majd meghívják Műveletnév.
Példa
Az alábbi példakód azt mutatja be, hogy a Dns osztály aszinkron metódusaival kéri le a tartománynévrendszer adatait egy felhasználó által megadott számítógépen. Vegye figyelembe, hogy a null (Nothing a Visual Basicben) a BeginGetHostByName,requestCallback és stateObject paraméterekhez van továbbítva, mivel ezek az argumentumok nem szükségesek ennek a megközelítésnek a használatakor.
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computer.
*/
using System;
using System.Net;
using System.Net.Sockets;
namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class BlockUntilOperationCompletes
{
public static void Main(string[] args)
{
// Make sure the caller supplied a host name.
if (args.Length == 0 || args[0].Length == 0)
{
// Print a message and exit.
Console.WriteLine("You must specify the name of a host computer.");
return;
}
// Start the asynchronous request for DNS information.
// This example does not use a delegate or user-supplied object
// so the last two arguments are null.
IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);
Console.WriteLine("Processing your request for information...");
// Do any additional work that can be done here.
try
{
// EndGetHostEntry blocks until the process completes.
IPHostEntry host = Dns.EndGetHostEntry(result);
string[] aliases = host.Aliases;
IPAddress[] addresses = host.AddressList;
if (aliases.Length > 0)
{
Console.WriteLine("Aliases");
for (int i = 0; i < aliases.Length; i++)
{
Console.WriteLine($"{aliases[i]}");
}
}
if (addresses.Length > 0)
{
Console.WriteLine("Addresses");
for (int i = 0; i < addresses.Length; i++)
{
Console.WriteLine($"{addresses[i].ToString()}");
}
}
}
catch (SocketException e)
{
Console.WriteLine($"An exception occurred while processing the request: {e.Message}");
}
}
}
}
' The following example demonstrates using asynchronous methods to
' get Domain Name System information for the specified host computer.
Imports System.Net
Imports System.Net.Sockets
Namespace Examples.AdvancedProgramming.AsynchronousOperations
Public Class BlockUntilOperationCompletes
Public Shared Sub Main(args() as String)
' Make sure the caller supplied a host name.
If (args.Length = 0)
' Print a message and exit.
Console.WriteLine("You must specify the name of a host computer.")
End
End If
' Start the asynchronous request for DNS information.
' This example does not use a delegate or user-supplied object
' so the last two arguments are Nothing.
Dim result as IAsyncResult = Dns.BeginGetHostEntry(args(0), Nothing, Nothing)
Console.WriteLine("Processing your request for information...")
' Do any additional work that can be done here.
Try
' EndGetHostByName blocks until the process completes.
Dim host as IPHostEntry = Dns.EndGetHostEntry(result)
Dim aliases() as String = host.Aliases
Dim addresses() as IPAddress = host.AddressList
Dim i as Integer
If aliases.Length > 0
Console.WriteLine("Aliases")
For i = 0 To aliases.Length - 1
Console.WriteLine("{0}", aliases(i))
Next i
End If
If addresses.Length > 0
Console.WriteLine("Addresses")
For i = 0 To addresses.Length - 1
Console.WriteLine("{0}", addresses(i).ToString())
Next i
End If
Catch e as SocketException
Console.WriteLine("An exception occurred while processing the request: {0}", e.Message)
End Try
End Sub
End Class
End Namespace