CompletedEventHandler Delegasikan
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mewakili metode yang akan menangani Completed peristiwa.
public delegate void CompletedEventHandler(System::Object ^ sender, CompletedEventArgs ^ e);
public delegate void CompletedEventHandler(object sender, CompletedEventArgs e);
type CompletedEventHandler = delegate of obj * CompletedEventArgs -> unit
Public Delegate Sub CompletedEventHandler(sender As Object, e As CompletedEventArgs)
Parameter
- sender
- Object
Instans objek untuk memanggil metode ini.
CompletedEventArgs yang menentukan alasan peristiwa dipanggil.
Contoh
Contoh berikut memanggil metode secara asinkron. Metode Win32_Process.Create dipanggil untuk membuat proses baru untuk Calc.exe.
using System;
using System.Management;
public class InvokeMethodAsync
{
private bool isComplete = false;
private ManagementBaseObject returnObject;
public InvokeMethodAsync()
{
// Get the object on which the method
// will be invoked
ManagementClass processClass =
new ManagementClass("Win32_Process");
// Create a results and completion handler
ManagementOperationObserver handler =
new ManagementOperationObserver();
handler.Completed +=
new CompletedEventHandler(this.Completed);
// Invoke method asynchronously
ManagementBaseObject inParams =
processClass.GetMethodParameters("Create");
inParams["CommandLine"] = "calc.exe";
processClass.InvokeMethod(
handler, "Create", inParams, null);
// Do something while method is executing
while(!this.IsComplete)
{
System.Threading.Thread.Sleep(1000);
}
}
// Property allows accessing the result
// object in the main function
private ManagementBaseObject ReturnObject
{
get
{
return returnObject;
}
}
// Delegate called when the method completes
// and results are available
private void NewObject(object sender,
ObjectReadyEventArgs e)
{
Console.WriteLine("New Object arrived!");
returnObject = e.NewObject;
}
// Used to determine whether the method
// execution has completed
private bool IsComplete
{
get
{
return isComplete;
}
}
private void Completed(object sender,
CompletedEventArgs e)
{
isComplete = true;
Console.WriteLine("Method invoked.");
}
public static void Main()
{
InvokeMethodAsync invokeMethod = new InvokeMethodAsync();
return;
}
}
Imports System.Management
Public Class InvokeMethodAsync
Private isFinished As Boolean = False
Private returnObj As ManagementBaseObject
Public Sub New()
' Get the object on which the method
' will be invoked
Dim processClass As ManagementClass = _
New ManagementClass("Win32_Process")
' Create a results and completion handler
Dim handler As ManagementOperationObserver = _
New ManagementOperationObserver
AddHandler handler.Completed, _
AddressOf Me.Completed
' Invoke method asynchronously
Dim inParams As ManagementBaseObject = _
processClass.GetMethodParameters("Create")
inParams("CommandLine") = "calc.exe"
processClass.InvokeMethod( _
handler, "Create", inParams, Nothing)
' Do something while method is executing
While (Not Me.IsComplete)
System.Threading.Thread.Sleep(1000)
End While
End Sub
' Property allows accessing the result
' object in the main function
Private Function ReturnObject() As ManagementBaseObject
Return returnObj
End Function
' Delegate called when the method completes
' and results are available
Private Sub NewObject(ByVal sender As Object, _
ByVal e As ObjectReadyEventArgs)
Console.WriteLine("New Object arrived!")
returnObj = e.NewObject
End Sub
' Used to determine whether the method
' execution has completed
Private Function IsComplete() As Boolean
Return isFinished
End Function
Private Sub Completed(ByVal sender As Object, _
ByVal e As CompletedEventArgs)
isFinished = True
Console.WriteLine("Completed method invocation.")
End Sub
Public Shared Function _
Main(ByVal args() As String) As Integer
Dim invokeMethod As New InvokeMethodAsync
Return 0
End Function
End Class
Metode Ekstensi
GetMethodInfo(Delegate) |
Mendapatkan objek yang mewakili metode yang diwakili oleh delegasi yang ditentukan. |
Berlaku untuk
Berkolaborasi dengan kami di GitHub
Sumber untuk konten ini dapat ditemukan di GitHub, yang juga dapat Anda gunakan untuk membuat dan meninjau masalah dan menarik permintaan. Untuk informasi selengkapnya, lihat panduan kontributor kami.