Bir AsyncWaitHandle Kullanarak Uygulamanın Yürütülmesini Engelleme
Zaman uyumsuz bir işlemin sonuçlarını beklerken başka işler yapmaya devam emeyen uygulamaların işlem tamamlanana kadar engellemesi gerekir. Zaman uyumsuz işlemin tamamlanmasını beklerken uygulamanızın ana iş parçacığını engellemek için aşağıdaki seçeneklerden birini kullanın:
AsyncWaitHandle Zaman uyumsuz işlemin BeginOperationName yöntemi tarafından döndürülen özelliğini IAsyncResult kullanın. Bu yaklaşım bu konuda gösterilmiştir.
Zaman uyumsuz işlemin EndOperationName yöntemini çağırın. Bu yaklaşımı gösteren bir örnek için bkz . Zaman Uyumsuz İşlemi Sonlandırarak Uygulama Yürütmeyi Engelleme.
Zaman uyumsuz bir işlem tamamlanana kadar engellemek için bir veya daha fazla WaitHandle nesne kullanan uygulamalar genellikle BeginOperationName yöntemini çağırır, işlemin sonuçları olmadan yapılabilecek tüm işleri gerçekleştirir ve zaman uyumsuz işlemler tamamlanana kadar engeller. Bir uygulama, kullanarak AsyncWaitHandleyöntemlerden birini WaitOne çağırarak tek bir işlemi engelleyebilir. Zaman uyumsuz işlemler kümesinin tamamlanmasını beklerken engellemek için, ilişkili AsyncWaitHandle nesneleri bir dizide depolayın ve yöntemlerden birini çağırın WaitAll . Zaman uyumsuz işlem kümelerinden herhangi birinin tamamlanmasını beklerken engellemek için, ilişkili AsyncWaitHandle nesneleri bir dizide depolayın ve yöntemlerden birini çağırın WaitAny .
Örnek
Aşağıdaki kod örneği, kullanıcı tarafından belirtilen bir bilgisayarın Etki Alanı Adı Sistemi bilgilerini almak için DNS sınıfında zaman uyumsuz yöntemlerin kullanılmasını gösterir. Örnekte, zaman uyumsuz işlemle ilişkili kullanılarak engelleme WaitHandle gösterilmektedir. Bu null
yaklaşım kullanılırken gerekli olmadığından ve stateObject
parametreleri için BeginGetHostByNamerequestCallback
(Nothing
Visual Basic'te) geçirildiğini unutmayın.
/*
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;
using System.Threading;
namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class WaitUntilOperationCompletes
{
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.
IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);
Console.WriteLine("Processing request for information...");
// Wait until the operation completes.
result.AsyncWaitHandle.WaitOne();
// The operation completed. Process the results.
try
{
// Get the results.
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("{0}", aliases[i]);
}
}
if (addresses.Length > 0)
{
Console.WriteLine("Addresses");
for (int i = 0; i < addresses.Length; i++)
{
Console.WriteLine("{0}",addresses[i].ToString());
}
}
}
catch (SocketException e)
{
Console.WriteLine("Exception occurred while processing the request: {0}",
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
Imports System.Threading
namespace Examples.AdvancedProgramming.AsynchronousOperations
Public Class WaitUntilOperationCompletes
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.
Dim result as IAsyncResult = Dns.BeginGetHostEntry(args(0), Nothing, Nothing)
Console.WriteLine("Processing request for information...")
' Wait until the operation completes.
result.AsyncWaitHandle.WaitOne()
' The operation completed. Process the results.
Try
' Get the results.
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
Ayrıca bkz.
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin