silverlight 中的 userState 参数

本文详细介绍了AsyncCompletedEventArgs.UserState属性的作用、用途及示例应用,旨在帮助开发者掌握如何通过该属性追踪异步操作的生存期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AsyncCompletedEventArgs . . :: .UserState 属性

获取异步任务的唯一标识符。

命名空间:  System.ComponentModel

程序集:  System(在 System.dll 中)

类型:System..::.Object

唯一标识异步任务的对象引用;如果未设置任何值,则为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

此属性的值是在对启动任务的异步方法进行初始调用时设置的。


 示例

下面的代码示例演示如何使用 AsyncOperation 来跟踪异步操作的生存期。此代码示例摘自一个为 System.ComponentModel..::.AsyncOperationManager 类提供的更大的示例。

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;


...


// This event handler updates the ListView control when the
// PrimeNumberCalculator raises the CalculatePrimeCompleted
// event. The ListView item is updated with the appropriate
// outcome of the calculation: Canceled, Error, or result.
private void primeNumberCalculator1_CalculatePrimeCompleted(
    object sender, 
    CalculatePrimeCompletedEventArgs e)
{
    Guid taskId = (Guid)e.UserState;

    if (e.Cancelled)
    {   
        string result = "Canceled";

        ListViewItem lvi = UpdateListViewItem(taskId, result);

        if (lvi != null)
        {
            lvi.BackColor = Color.Pink;
            lvi.Tag = null;
        }
    }
    else if (e.Error != null)
    {
        string result = "Error";

        ListViewItem lvi = UpdateListViewItem(taskId, result);

        if (lvi != null)
        {
            lvi.BackColor = Color.Red;
            lvi.ForeColor = Color.White;
            lvi.Tag = null;
        }
    }
    else
    {   
        bool result = e.IsPrime;

        ListViewItem lvi = UpdateListViewItem(
            taskId, 
            result, 
            e.FirstDivisor);

        if (lvi != null)
        {
            lvi.BackColor = Color.LightGray;
            lvi.Tag = null;
        }
    }
}

摘自MSDN: http://technet.microsoft.com/zh-cn/magazine/system.componentmodel.asynccompletedeventargs.userstate%28VS.90%29.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值