Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Clase AsyncOperation

 

Publicado: octubre de 2016

Realiza un seguimiento de la duración de una operación asincrónica.

Espacio de nombres:   System.ComponentModel
Ensamblado:  System (en System.dll)

System.Object
  System.ComponentModel.AsyncOperation

[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public sealed class AsyncOperation

NombreDescripción
System_CAPS_pubpropertySynchronizationContext

Obtiene el objeto SynchronizationContext que se pasó al constructor.

System_CAPS_pubpropertyUserSuppliedState

Obtiene o establece un objeto que se usa para identificar de forma única una operación asincrónica.

NombreDescripción
System_CAPS_pubmethodEquals(Object)

Determina si el objeto especificado es igual al objeto actual.(Heredado de Object).

System_CAPS_protmethodFinalize()

Finaliza la operación asincrónica.(Invalida Object.Finalize()).

System_CAPS_pubmethodGetHashCode()

Sirve como la función hash predeterminada.(Heredado de Object).

System_CAPS_pubmethodGetType()

Obtiene el Type de la instancia actual.(Heredado de Object).

System_CAPS_pubmethodOperationCompleted()

Pone fin a la duración de una operación asincrónica.

System_CAPS_pubmethodPost(SendOrPostCallback, Object)

Invoca un delegado en el subproceso o contexto adecuado para el modelo de aplicaciones.

System_CAPS_pubmethodPostOperationCompleted(SendOrPostCallback, Object)

Pone fin a la duración de una operación asincrónica.

System_CAPS_pubmethodToString()

Devuelve una cadena que representa al objeto actual. (Heredado de Object).

When you implement a class according to the Event-based Asynchronous Pattern Overview, you may need to track the lifetime of each asynchronous operation invoked on an instance of your class. The T:System.ComponentModel.AsyncOperation class provides ways to track and report the progress of an asynchronous task.

The following list identifies ways to use an T:System.ComponentModel.AsyncOperation object:

  • To report progress and interim results to the client, call M:System.ComponentModel.AsyncOperation.Post(System.Threading.SendOrPostCallback,System.Object) from your asynchronous worker code.

  • To indicate that an asynchronous task has completed, or to cancel a pending asynchronous task, call M:System.ComponentModel.AsyncOperation.PostOperationCompleted(System.Threading.SendOrPostCallback,System.Object).

Your class should get an T:System.ComponentModel.AsyncOperation object for each asynchronous task by calling M:System.ComponentModel.AsyncOperationManager.CreateOperation(System.Object) when each task starts. To allow the client to distinguish separate asynchronous tasks, M:System.ComponentModel.AsyncOperationManager.CreateOperation(System.Object) takes a parameter for a unique client-provided token, which becomes the P:System.ComponentModel.AsyncOperation.UserSuppliedState property. It can then be used by client code to identify the particular asynchronous task that is raising progress or completion events.

Notas para desarrolladores de herederos:

Implementers must ensure the M:System.ComponentModel.AsyncOperation.PostOperationCompleted(System.Threading.SendOrPostCallback,System.Object) and M:System.ComponentModel.AsyncOperation.Post(System.Threading.SendOrPostCallback,System.Object) invocations are asynchronous, so that class library providers do not need to concern themselves with potential stack overflows if they assume asynchronous behavior in a particular application model that happens to be synchronous.

For more information about implementing asynchronous classes, see Implementing the Event-based Asynchronous Pattern.

The following code example demonstrates using an T:System.ComponentModel.AsyncOperation object to track the lifetime of asynchronous operations. This code example is part of a larger example provided for the T:System.ComponentModel.AsyncOperationManager class.

For a full code listing, see How to: Implement a Component that Supports the Event-based Asynchronous Pattern. For a full code listing of a client form, see How to: Implement a Client of the Event-based Asynchronous Pattern.

// This method starts an asynchronous calculation. 
// First, it checks the supplied task ID for uniqueness.
// If taskId is unique, it creates a new WorkerEventHandler 
// and calls its BeginInvoke method to start the calculation.
public virtual void CalculatePrimeAsync(
    int numberToTest,
    object taskId)
{
    // Create an AsyncOperation for taskId.
    AsyncOperation asyncOp =
        AsyncOperationManager.CreateOperation(taskId);

    // Multiple threads will access the task dictionary,
    // so it must be locked to serialize access.
    lock (userStateToLifetime.SyncRoot)
    {
        if (userStateToLifetime.Contains(taskId))
        {
            throw new ArgumentException(
                "Task ID parameter must be unique", 
                "taskId");
        }

        userStateToLifetime[taskId] = asyncOp;
    }

    // Start the asynchronous operation.
    WorkerEventHandler workerDelegate = new WorkerEventHandler(CalculateWorker);
    workerDelegate.BeginInvoke(
        numberToTest,
        asyncOp,
        null,
        null);
}

Plataforma universal de Windows
Disponible desde 10
.NET Framework
Disponible desde 2.0
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0

Cualquier miembro ( Compartido en Visual Basic) estático público de este tipo es seguro para subprocesos. No se garantiza que los miembros de instancia sean seguros para subprocesos.

Volver al principio
Mostrar: