簡體   English   中英

具有線程的C#RSS feed閱讀器,將feed多次寫入listView

[英]C# RSS feed reader with thread, feeds are written to listView multiple times

我有一個RSS新聞閱讀器,它可以讀取rss提要,並在ListView中使用鏈接寫入新聞。 當我執行程序時,我正在啟動一個新線程,如下所示:

    Thread myThread = new Thread(getNews);
    myThread.Start();

我讀取提要的方法如下所示:

    public void getNews()
    {  
        //Creates a XmlTextReader which reads from the url entered in input field
        rssReader = new XmlTextReader(txtUrl.Text);

        //Creates an xml doc to save the content of the entered path
        rssDoc = new XmlDocument();

        //Loads the xml content from the reader into a XmlDocument
        rssDoc.Load(rssReader);

        //Make a loop to search for the <rss> tag
        for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
        {
            //If the childenode is the rss tag
            if (rssDoc.ChildNodes[i].Name == "rss")
            {
                //the <rss> tag is found, and we know where it is
                nodeRss = rssDoc.ChildNodes[i];
            }
        }

        //Make a loop to search for the <channel> tag
        for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
        {
            //If the childnode is the channel tag
            if (nodeRss.ChildNodes[i].Name == "channel")
            {
                //The channel tag is found and we know where it is
                nodeChannel = nodeRss.ChildNodes[i];
            }
        }

        //Make a loop to search for the <item> tag
        for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
        {
            //If the childnode is the item tag
            if (nodeChannel.ChildNodes[i].Name == "item")
            {
                //the item tag is found, and we know where it is
                nodeItem = nodeChannel.ChildNodes[i];

                //Creates a new row in the LstView which contains information from inside the nodes
                rowNews = new ListViewItem();
                rowNews.Text = nodeItem["title"].InnerText;
                rowNews.SubItems.Add(nodeItem["link"].InnerText);

                if (this.lstView.InvokeRequired)
                {
                    AddItemCallback d = new AddItemCallback(getNews);
                    this.Invoke(d);
                }
                else
                {
                    lstView.Items.Add(rowNews);
                }
            }

        }

我的問題是,在我開始在新線程中運行代碼並使用委托后,檢查listView是否要求被調用的所有新聞提要多次寫入我的listView中。 如果我在不啟動新線程的情況下運行方法並且僅使用委托編寫一次該委托,那為什么呢? 這可能是一個非常簡單的問題,但無法找出原因

在此先感謝,感謝代碼示例:)

如果需要invoke,那么您將再次調用getNews方法。 而不是使用此代碼:

if (this.lstView.InvokeRequired)
{
     AddItemCallback d = new AddItemCallback(getNews); // STOP CALLING getNews
     this.Invoke(d);
}

您需要調用另一種方法來更新您的UI。 這是一個巨大的循環。

您將調用檢查放在錯誤的位置–因此,調用getNews函數的次數比預期的要多得多。

在getNews的頂部嘗試以下操作:

if (this.lstView.InvokeRequired) {
AddItemCallback d = new AddItemCallback(getNews);
this.Invoke(d);
return;
}

然后在末尾將if語句替換為lstView.Items.Add(rowNews);

當然,通過這種方式可以有效地將getNews編組到UI線程。 您真正需要的是構建ListViewItems的集合並將該集合傳遞給Updater方法。在該方法中,您檢查是否需要調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM