如何从Background Worker的事件args中获取值?

时间:2014-08-20 17:20:17

标签: c# winforms backgroundworker

在方法中,我填充ID列表并将它们传递给我的Background Worker。当我尝试访问此对象时,我获得了空值,但我并不知道原因。

    public void Submit()
    {
        if (DataModels.Count == 0)
            return;

        List<long> docRefIds = new List<long>();

        foreach (var row in DataModels)
        {
            if (row.PrintAck == true)
                docRefIds.Add(Convert.ToInt32(row.DOCUMENT_REF_ID));
        }

        BackgroundWorker worker_Acknowledge = new BackgroundWorker();
        worker_Acknowledge.DoWork += new DoWorkEventHandler(worker_Acknowledge_DoWork);
        worker_Acknowledge.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(worker_Acknowledge_RunWorkerCompleted);
        worker_Acknowledge.RunWorkerAsync(docRefIds.ToArray());
    }

然后在DoWork活动中......

    private void worker_Acknowledge_DoWork(object sender, DoWorkEventArgs e)
    {
        var wkService = WorkItem.Services.Get<MyService>();

        if (wkService == null)
            return;

        long?[] ids = e.Argument as long?[];
        string errorMessage;
        e.Result = wkService.SaveDetails(ids, out errorMessage);
    }

我在Submit方法的最后一行设置了一个断点,docRefIds拥有我期望的有效数据。实际上,e.Argument具有4个值,但在该行执行后ids为空。

1 个答案:

答案 0 :(得分:1)

当然在耗尽资源并最终在这里发帖后,我意识到自己的错误。

我的列表不可为空,因此DoWork活动中的演员表无法正常工作。在我的Submit方法中,列表应该声明为:

    List<long?> docRefIds = new List<long?>();