【问题标题】:How can i report from backgroundworker dowork event to listView and also to toolStripStatusLabel?我如何从 backgroundworker dowork 事件向 listView 和 toolStripStatusLabel 报告?
【发布时间】:2015-03-03 06:41:04
【问题描述】:

我有这个方法,我从后台工作人员 dowork 事件中调用它:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            GetForumsInfo();
        }

方法:

string res;
        private void GetForumsInfo()
        {
            int countResults = 0;
            int index = 0;
            int index1 = 0;
            List<string> forumsNames = new List<string>();
            string[] lines = File.ReadAllLines(@"C:\testhtml\htmlloaded.txt");
            List<string> ttt = new List<string>();
            for (int i = 0; i < lines.Length; i++)
            {
                Regex myTitle = new Regex("(?<=title=\").*?(?=\"\\>)");
                //string strTargetString = @"<a href=""/Forums2008/forumPage.aspx?forumId=690"" title=""ישראלים בקנדה"">ישראלים בקנדה</a>" + "\n" + @" ";

                if (lines[i].Contains("Forums2008/forumPage.aspx?forumId="))
                {
                    string firstTag = "Forums2008/forumPage.aspx?forumId=";
                    string lastTag = "title";
                    int indx = lines[i].IndexOf(firstTag);
                    int indx2 = lines[i].IndexOf(lastTag, indx);
                    res = lines[i].Substring(indx + 34, indx2 - indx - 36);
                    string titleResult = myTitle.Match(lines[i]).Value;

                    string endTag = "</a>";


                    index = forums.IndexOf(firstTag, index1);

                    if (index == -1)
                        continue;

                    var secondIndex = forums.IndexOf(endTag, index);*/
                    StreamWriter w = new StreamWriter(@"c:\testmytext\tt.txt");
                    w.WriteLine(titleResult);
                    w.Close();
                    if (!forumsNames.Contains(titleResult))
                    {
                        if (!titleResult.Contains("&quot;"))
                        {
                                arr[0] = titleResult;//"product_1";
                                arr[1] = res;

                                itm = new ListViewItem(arr);
                                backgroundWorker1.ReportProgress(0, res);

                            ttt.Add(res);
                            countResults++;
                            string SummaryText = String.Format("Forum Name {0} / {1}",
                                                               titleResult, countResults);
                            //backgroundWorker1.ReportProgress(0, SummaryText);//titleResult);
                            forumsNames.Add(titleResult);
                        }
                    }
                    index1 = index + 1;
                }
            }
            numberofforums = forumsNames.Count;
            SaveToListView();
        }

在我报告 SummaryText 之前,我现在只报告变量 res。 这是backgroundworker进度改变事件的代码:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //toolStripStatusLabel1.Text = e.UserState.ToString();
            listView1.Items.Add(e.UserState.ToString());
        }

但现在我想向 UserState 报告三个对象:

  1. 变量 res
  2. 变量titleResult
  3. 变量 SummaryText 变量。

并在 progressChanged 事件中使用 res 和 titleResult 更新列表视图,以及使用 SummaryText 更新 toolStripStatusLabel1。

尝试谷歌,但我不明白如何将多个参数传递给 reportprogress 以及如何在 progressChanged 中更新它。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    创建一个类,其中包含要传递给 ProgressChanged-Event 的属性,例如:

    internal class BachgroundWorkerProgressItem
    {
      internal BachgroundWorkerProgressItem(string res, string titleResult, string summaryText)
      {
        Res = res;
        TitleResult = titleResult;
        SummaryText = summaryText:
      }
    
      internal string Res { get; private set; }
      internal string TitleResult { get; private set; }
      internal string SummaryText { get; private set; }
    }
    

    这种类型的对象通过 UserState 传递给 ProgressChanged-Event。如果您必须:

    BachgroundWorkerProgressItem item = (BachgroundWorkerProgressItem)e.UserState;
    

    如果您不想在其他任何地方使用它,您也可以将 BachgroundWorkerProgressItem 创建为嵌套类并将其设为私有。

    要传递这种类型的对象,您必须像这样调用 ReportProgress:

    backgroundWorker1.ReportProgress(0, new BachgroundWorkerProgressItem(res, titleResult, summaryText));
    

    【讨论】:

    • Tomtom 我遇到了异常。在 progressChanged 事件中,我添加了以下行: BachgroundWorkerProgressItem item = (BachgroundWorkerProgressItem)e.UserState;我在这一行遇到异常: InvalidCastException: Unable to cast object of type 'System.String' to type 'BachgroundWorkerProgressItem
    • 您是否将对象正确传递给 ReportProgress-Method?我已经更新了我的帖子
    猜你喜欢
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多