【问题标题】:Pass arguments to BackGroundWorker将参数传递给 BackGroundWorker
【发布时间】:2012-11-15 10:39:29
【问题描述】:

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

  1. 文件夹名称:字符串
  2. FTP 计数:整数
  3. 预期计数:整数

所有都相互关联(基于它们在列表中的索引),我想将它们作为参数传递给BackGroundWorker,它在增加 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.

【问题讨论】:

  • 你有一个Application.DoEvents() 电话的事实意味着你的方法是有缺陷的。 :( 可能需要做一些改造。
  • 我知道并且我正在努力(你能为此提出一些建议吗?)。现在我需要关于传递参数的帮助。

标签: c# backgroundworker


【解决方案1】:

使用指定参数的DoWork(Object data) method。您必须进行投射并从args Argument Property 检索它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    相关资源
    最近更新 更多