lundi 2 mars 2015

How can i report from backgroundworker dowork event to listView and also to toolStripStatusLabel?

I have this method i'm calling it from backgroundworker dowork event:



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


The method:



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();
}


Before i was reporting SummaryText now i'm reporting only the variable res. This is the code of the backgroundworker progress changed event:



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


But now i want to report three objects to the UserState:



  1. The variable res

  2. The variable titleResult

  3. The variable SummaryText variable.


And in the progressChanged event to update the listview with the res and titleResult and the toolStripStatusLabel1 to update with the SummaryText.


Tried to google but i didn't understand how to pass more then one param to the reportprogress and how to update it in the progressChanged.


Aucun commentaire:

Enregistrer un commentaire