微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight 中的 userState 参数

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐