您如何通过列表<>的列表<>到后台工作者?

问题描述:

我有一个backgroundWorker正在处理两个列表.我需要将清单传递给工人.结果是空列表.

I have a backgroundWorker that is processing two lists. I need to pass the Lists to the worker. the result is empty lists.

传递列表(和2个其他参数)的代码.在我的测试中,每个列表有20多个项目,而 List<> 项目显示,在调用之前,这20多个项目是完整的.他们在检查员中说算".然后是项目数.

Code to pass the Lists (and 2 other parameters). In my test, each list has 20+ items, and the List<> items show that the 20+ items are intact just prior to the call. In the inspector they say "Count" followed by the number of items.

List<Object> arguments = new List<object>();

// Add arguments to pass to background worker
arguments.Add(managerSource);
arguments.Add(managerDestination);
arguments.Add(source as List<EntityTypeContainer>);
arguments.Add(destination as List<EntityTypeContainer>);
// Invoke the backgroundWorker
Main.bgWorkerCopyEntityTypes.RunWorkerAsync(arguments);

我已经调试了backgroundWorker的DoWork方法.在接收到参数参数时,它们已经是空的".空表示我的意思是参数 List<> 中的条目显示为"Count -0".如果将它们移到强制转换变量中,它们仍为0.我已在下面列出了代码,但是一旦调用该方法,问题就会立即显现出来.

I have debugged the DoWork method of the backgroundWorker. On receiving the arguments parameter, they are already "empty". By empty I mean that the entries in the argument List<> show as "Count 0". If I move them into cast variables, they are still 0. I have listed the code below, but the problem manifests itself as soon as the method is invoked.

   private void bgWorkerCopyEntityTypes_DoWork(object sender, DoWorkEventArgs e)
    {
        List<object> arguments = (List<object>)e.Argument;
        RemoteManager managerSource = (RemoteManager)arguments[0];
        RemoteManager managerDestination = (RemoteManager)arguments[1];
        List<EntityTypeContainer> source = (List<EntityTypeContainer>)arguments[2];
        List<EntityTypeContainer> destination = (List<EntityTypeContainer>)arguments[3];

任何帮助表示赞赏!

为参数创建一个类:

public class BgwArgs
{
    public string ManagerSource { get; set; }
    public string ManagerDestination { get; set; }
    public List<EntityTypeContainer> Source { get; set; }
    public List<EntityTypeContainer> Destination { get; set; }

}

制作该类的实例并填充它:

Make an instance of that class and populate it:

var bgwargs = new BgwArgs();
bgwargs.ManagerSource = "whatever";
// etc.

通过:

Main.bgWorkerCopyEntityTypes.RunWorkerAsync(bgwargs);

然后获取其成员:

private void bgWorkerCopyEntityTypes_DoWork(object sender, DoWorkEventArgs e)
{
    var myArgs = (BgwArgs)e.Argument;
    var managerSource = myArgs.ManagerSource;
    // etc.
}