将参数传递给BackGroundWorker

时间:2012-11-15 02:22:50

标签: c# backgroundworker

我有以下类型的3个列表:

  1. folderName:String
  2. FTPcount:Int
  3. Expectedcount:Int
  4. 所有这些都是相互关联的(基于它们在列表中的索引),我想将它们作为参数传递给BackGroundWorker,它读取folderName,同时增加FTPcount和Expectedcount。

    我如何传递它们(通过引用?),以便在每个执行完成后我能看到增量?

            List<string> folderName = new List<string>();
            List<int> Expectedcount = new List<int>();
            List<int> FTPcount = new List<int>();
    
            foreach (string folder in Properties.Settings.Default.Folders)
            {
                BackgroundWorker bg = new BackgroundWorker();
    
                bGs.Add(bg);
                Expectedcount.Add(new int());
                FTPcount.Add(new int());
                folderName.Add(folder);
    
                bg.DoWork += new DoWorkEventHandler(Process_Instiution);
                bg.RunWorkerAsync(new { folderName = folder, Expected = Expectedcount[Expectedcount.Count - 1] as object, FTP = FTPcount[FTPcount.Count - 1] as object });
            }
    
            while (true)
            {
                bool IsBusy = false;
                foreach (BackgroundWorker bg in bGs)
                    if (bg.IsBusy)
                    {
                        IsBusy = true;
                        break;
                    }
    
                if (IsBusy)
                    Application.DoEvents();
                else
                    break;
            }
            //Code to read the various List and see how many files were expected and how many were FTPed.
    

1 个答案:

答案 0 :(得分:0)

使用具体参数的DoWork(Object data) method。您必须进行强制转换并从args Argument Property检索它。

我要么创建一个对象包装器来传递数据,要么使用匿名类型。