如何从backgroundworker dowork事件向listView以及toolStripStatusLabel报告?

时间:2015-03-03 06:41:04

标签: c# .net winforms

我有这个方法我从backgroundworker 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变量。
  4. 在progressChanged事件中,使用res和titleResult更新listview,使用SummaryText更新toolStripStatusLabel1。

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

1 个答案:

答案 0 :(得分:2)

创建一个类,其中包含要传递给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));