C# RSS跨线程

设计一个RSS阅读器,主窗体嵌入三个子窗体,分别是Treeview,listview,axWebBrowser,在Treeview控件中点击任一RSS条目,在listview显示这个条目下面的子条目,在axWebBrowser显示子条目的全文内容,
出现问题是:在Treeview控件中点击任一RSS条目,提示:(用户代码未处理InvalidOperationException
线程间操作无效: 从不是创建控件“TopicList”的线程访问它。
如何跨线程调用Windows窗体控件,断点是catch(Exception ex) {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message + " : " + ex.StackTrace);),
TopicList是装listview控件的窗体名称,MainForm是主窗体名称
TopicList全部代码:using System;
using System.Drawing;
using System.Xml;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI;

using RSSReader.Core;
using RSSReader.Controls.Engine;

namespace RSSReader.Controls
{

/// <summary>
/// 显示主题列表
/// </summary>
/// <remarks>
/// 20070401 ae
/// </remarks>
public class TopicList : DockContent
{
#region Auto Generate
private System.Windows.Forms.ColumnHeader colMain;
private System.Windows.Forms.ColumnHeader colTitle;
private System.Windows.Forms.ColumnHeader colAuthor;
private System.Windows.Forms.ColumnHeader colLink;
private System.Windows.Forms.ListView listTopic;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader colPubDate;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public TopicList() {
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing ) {
if( disposing ) {
if(components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TopicList));
this.colMain = new System.Windows.Forms.ColumnHeader();
this.colTitle = new System.Windows.Forms.ColumnHeader();
this.colPubDate = new System.Windows.Forms.ColumnHeader();
this.colAuthor = new System.Windows.Forms.ColumnHeader();
this.colLink = new System.Windows.Forms.ColumnHeader();
this.listTopic = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// colMain
//
this.colMain.Text = "!";
this.colMain.Width = 25;
//
// colTitle
//
this.colTitle.Text = "标题";
this.colTitle.Width = 300;
//
// colPubDate
//
this.colPubDate.Text = "创建日期";
this.colPubDate.Width = 120;
//
// colAuthor
//
this.colAuthor.Text = "作者";
//
// colLink
//
this.colLink.Text = "链接";
this.colLink.Width = 180;
//
// listTopic
//
this.listTopic.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.colMain,
this.colTitle,
this.colAuthor,
this.colPubDate,
this.colLink});
this.listTopic.Dock = System.Windows.Forms.DockStyle.Fill;
this.listTopic.FullRowSelect = true;
this.listTopic.GridLines = true;
this.listTopic.HideSelection = false;
this.listTopic.Location = new System.Drawing.Point(0, 3);
this.listTopic.Name = "listTopic";
this.listTopic.Size = new System.Drawing.Size(337, 370);
this.listTopic.TabIndex = 0;
this.listTopic.View = System.Windows.Forms.View.Details;
//
// TopicList
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(337, 376);
this.Controls.Add(this.listTopic);
this.DockableAreas = ((WeifenLuo.WinFormsUI.DockAreas)(((((WeifenLuo.WinFormsUI.DockAreas.Float | WeifenLuo.WinFormsUI.DockAreas.DockLeft)
| WeifenLuo.WinFormsUI.DockAreas.DockRight)
| WeifenLuo.WinFormsUI.DockAreas.DockTop)
| WeifenLuo.WinFormsUI.DockAreas.DockBottom)));
this.DockPadding.Bottom = 3;
this.DockPadding.Top = 3;
this.HideOnClose = true;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "TopicList";
this.ShowHint = WeifenLuo.WinFormsUI.DockState.DockTopAutoHide;
this.Text = "主题列表";
this.Load += new System.EventHandler(this.TopicList_Load);
this.ResumeLayout(false);

}
#endregion
#endregion

private void TopicList_Load(object sender, System.EventArgs e){
EventManager.Handle("sourceList_AfterSelect", new System.Windows.Forms.TreeViewEventHandler(listTopic_Load));
//EventManager.Register("listTopic_SelectedIndexChanged", listTopic, "SelectedIndexChanged");
EventManager.Register("listTopic_DoubleClick", listTopic, "DoubleClick");
}

private void listTopic_Load(object sender, System.Windows.Forms.TreeViewEventArgs e){
TreeViewEventHandler eventHandle = new TreeViewEventHandler(LoadTopicList);
eventHandle.BeginInvoke(sender, e, null, null);
}

private void LoadTopicList(object sender, System.Windows.Forms.TreeViewEventArgs e){
if(e.Node.Tag.ToString().IndexOf("URL:") != -1) {
if(!ConfigureInfo.CurrentSelectedNodeTag.Equals(e.Node.Tag.ToString())) {
ConfigureInfo.CurrentSelectedNodeTag = e.Node.Tag.ToString();
FetchFeedInfo(e.Node.Tag.ToString().Substring(e.Node.Tag.ToString().IndexOf("URL:")+4) ,"item");
}
}
}

public void FetchFeedInfo(string strFeedURL, string searchNodeText) {
try {
if(!searchNodeText.Trim().Equals("")) {
App.MainForm.statusBar.Text = "读入资源主题列表...";
ClearFeedList();
this.Cursor=Cursors.WaitCursor;

ListViewItem lstFeedItem;
ServiceResponseInfoItem infoItem;
ArrayList listItem = RssService.CallRssService(strFeedURL);
for(int i = 0; i < listItem.Count; i++){
lstFeedItem = new ListViewItem();
infoItem = listItem[i] as ServiceResponseInfoItem;
lstFeedItem.Text = Convert.ToString(listTopic.Items.Count+1);
lstFeedItem.SubItems.Add(infoItem.Title);
lstFeedItem.SubItems.Add(infoItem.Author);
lstFeedItem.SubItems.Add(infoItem.PubDate);
lstFeedItem.SubItems.Add(infoItem.Link);
lstFeedItem.Tag = infoItem.Link;
listTopic.Items.Add(lstFeedItem);
if(i == 0){
listTopic.Items[0].Selected = true;
}
}
App.MainForm.statusBar.Text = "完成";
this.Cursor=Cursors.Default;
}
}
catch(Exception ex) {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message + " : " + ex.StackTrace);
}
}

public void ClearFeedList() {
this.listTopic.Items.Clear();  
}
}
}
请高人帮助
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值