如何在RunWorkerCompletedEventArgs对象中设置UserState? [英] How do you set the UserState in the RunWorkerCompletedEventArgs object?

查看:216
本文介绍了如何在RunWorkerCompletedEventArgs对象中设置UserState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好. 我有一组运行Worker类实例的BackgroundWorker对象.当我调用Worker类时,对象实例即完成了事,然后用完了代码(循环结束).我能够监听RunWorkerCompleted()事件,但是当它调用我设置的委托时,我需要知道刚刚完成了哪些Worker对象.

HI all. I have an array of BackgroundWorker objects running instances of a Worker class. When I call the Worker class the object instance does it's thing and then runs out of code (the loop finishes). I'm able to listen to the RunWorkerCompleted() event but when it calls the delegate that I've set up I need to know which of my Worker objects just completed.

我在委托代表的RunWorkerCompletedEventArgs中看到一个UserState属性,但是在完成时我不知道如何在Worker对象中设置它.

I see a UserState property in the RunWorkerCompletedEventArgs that comes to my delegate but I have no idea how to set this in my Worker object as it's finishing.

有什么想法吗?

WorkManager.cs类中的片段

snippet from my WorkManager.cs class

public Worker AddWorker()
{
    Worker w = new Worker();

    _workers.Add(w.WorkerID,w);

    BackgroundWorker bg = new BackgroundWorker();
    _bgworkers.Add(bg);

    bg.DoWork += w.Start;
    bg.WorkerReportsProgress = true;
    bg.WorkerSupportsCancellation = true;
    bg.ProgressChanged += ProcessWorkerMessage;
    bg.RunWorkerCompleted += WorkerFinished;


    w.WorkManager = this;
    w.BackgroundWorker = bg;

    bg.RunWorkerAsync(w);


    return w;

}


public void WorkerFinished(object sender, RunWorkerCompletedEventArgs e)
{
    if (_onManagerEvent != null)
        _onManagerEvent(new ManagerEvent { EventDate = DateTime.Now, Message = "Worker ??? successfully ended." });
}

因此,当我的Worker对象在其Start()方法中完成循环时,该怎么办才能填充传递给我的WorkerFinished method()的RunWorkerCompleteEventArgs对象"e"的userState属性?

So when my Worker object finishes the loop in its Start() method, what do I do to fill the userState property of the RunWorkerCompleteEventArgs object "e" that is passed to my WorkerFinished method()?

谢谢

推荐答案

您在Worker类上的Start方法可以设置DoWorkEventArgs参数的Result属性.这是一个示例:

Your Start method on the Worker class can set the Result property of the DoWorkEventArgs argument. Here's an example:

void Start(object sender, DoWorkEventArgs e)
{
   //Do your loop and other work.
   e.Result = this;
}

然后在finish事件处理程序中,您可以检索e.Result:

Then in the finish event handler, you can retrieve e.Result:

public void WorkerFinished(object sender, RunWorkerCompletedEventArgs e)
{
    //You should always check e.Cancelled and e.Error before checking e.Result!
    // ... even though I'm skipping that here

    Worker w = e.Result as Worker;
    if( w != null)
    {
        if (_onManagerEvent != null)
            _onManagerEvent(new ManagerEvent 
                    { 
                      EventDate = DateTime.Now, 
                      Message = String.Format("Worker {0} successfully ended."
                                              , w.ToString()) 
                    });
    }
}

这篇关于如何在RunWorkerCompletedEventArgs对象中设置UserState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆