简体   繁体   English

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

[英]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: 我有这种方法,我是从backgroundworker dowork事件调用它的:

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. 在我报告SummaryText之前,我现在仅报告变量res。 This is the code of the backgroundworker progress changed event: 这是backgroundworker进度更改事件的代码:

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: 但是现在我想向UserState报告三个对象:

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

And in the progressChanged event to update the listview with the res and titleResult and the toolStripStatusLabel1 to update with the SummaryText. 并在progressChanged事件中使用res和titleResult更新listview,并使用SummaryText更新toolStripStatusLabel1。

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. 尝试使用谷歌,但我不明白如何将更多的参数传递给reportprogress以及如何在progressChanged中对其进行更新。

Create a class which holds the properties you want to pass to the ProgressChanged-Event like: 创建一个类,其中包含要传递给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; }
}

Objects of this type you pass to the ProgressChanged-Event over the UserState. 通过UserState传递给ProgressChanged-Event的这种类型的对象。 And in the event you have to: 并且,如果您必须:

BachgroundWorkerProgressItem item = (BachgroundWorkerProgressItem)e.UserState;

You can alse create the BachgroundWorkerProgressItem as a nested class and make it private if you don't want to use it anywhere else. 您还可以将BachgroundWorkerProgressItem创建为嵌套类,如果不想在其他任何地方使用它,可以将其设为私有。

To pass an object of this type you have to call the ReportProgress like: 要传递这种类型的对象,您必须像这样调用ReportProgress:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何报告其他班级的背景工作者功课? - How can i report a backgroundworker dowork from another class? 如何从backgroundworker dowork事件向dataGridView报告多个reportprogress值? - How can I report multiple reportprogress values from backgroundworker dowork event to a dataGridView? 我如何调用从backgroundworker dowork事件调用的方法? - How can i invoke a method called from backgroundworker dowork event? 如何在 backgroundworker dowork 事件中按百分比报告进度? - How to report progress by percentages in backgroundworker dowork event? 如何在后台工作者DoWork事件中启用/禁用按钮? - How can i enable/disable a button in a backgroundworker DoWork event? 我应该报告什么来报告后台工作DoWork活动的进展? - What should I report to report progress in the backgroundworker DoWork event? 如何使用backgroundworker向toolStripStatusLabel报告字符串值? - How do i report to toolStripStatusLabel text a string value using a backgroundworker? 我如何在 backgroundworker dowork 事件中使用 WebClient progresschanged 事件? - How do I use the WebClient progresschanged event from within backgroundworker dowork event? 我该如何为我的backgroundworker dowork事件添加经过时间的编程器? - How can i add a time elapsed progrerss to my backgroundworker dowork event? 如何从BackgroundWorker.DoWork事件获取更新 - How to get update from BackgroundWorker.DoWork Event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM