Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AsyncOperation.UserSuppliedState Property

Gets or sets an object used to uniquely identify an asynchronous operation.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

public Object UserSuppliedState { get; }
/** @property */
public Object get_UserSuppliedState ()

public function get UserSuppliedState () : Object

Not applicable.

Property Value

The state object passed to the asynchronous method invocation.

If your class supports multiple asynchronous methods or multiple invocations of a single asynchronous method, clients will need a way to determine which asynchronous task is raising events. Your MethodNameAsync method should take a parameter of type Object that will act as a task ID. You will use this task ID when you call the AsyncOperationManager.CreateOperation, method and this will associate the client's task ID with a particular invocation of your asynchronous operation. This task ID is made available to your implementation through the UserSuppliedState property.

Caution noteCaution:

Client code must be careful to provide a unique value for the UserSuppliedState property. Non-unique task IDs may cause your implementation to report progress and other events incorrectly. Your code should check for a non-unique task ID and raise an ArgumentException if one is detected.

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

// This method cancels a pending asynchronous operation.
public void CancelAsync(object taskId)
{
    AsyncOperation asyncOp = userStateToLifetime[taskId] as AsyncOperation;
    if (asyncOp != null)
    {   
        lock (userStateToLifetime.SyncRoot)
        {
            userStateToLifetime.Remove(taskId);
        }
    }
}

// This method cancels a pending asynchronous operation.
public void CancelAsync(Object taskId)
{
    Object obj = userStateToLifetime.get_Item(taskId);
    if (obj != null)
    {
        synchronized (userStateToLifetime.get_SyncRoot())
        {
            userStateToLifetime.Remove(taskId);
        }
    }
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.