【发布时间】: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("""))
{
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 报告三个对象:
- 变量 res
- 变量titleResult
- 变量 SummaryText 变量。
并在 progressChanged 事件中使用 res 和 titleResult 更新列表视图,以及使用 SummaryText 更新 toolStripStatusLabel1。
尝试谷歌,但我不明白如何将多个参数传递给 reportprogress 以及如何在 progressChanged 中更新它。
【问题讨论】: