利用后台进程System.ComponentModel.BackgroundWorker获取股票这样的资源信息

本文详细介绍了如何使用System.ComponentModel.BackgroundWorker在.NET应用中实现异步在线数据获取,避免影响主程序性能。通过实例展示了如何创建、配置和调用后台进程,并在必要时销毁该对象。

今天拜读了别人的代码,发现自己又一次孤陋寡闻.

System.ComponentModel提供了很多丰富的对象让编程更简单,很容易地达到你要实现的目的,避免自己写更多的逻辑来实现你要的需求。

假设我们的产品是一个网站,它需要获取internet上其它网站的信息,比如股票,天气预报。要获取这样的附属信息一般不能用主线程直接获取,因为要频繁地获取这样的信息势必会影响网

站的性能,那就用.net提供的后台进程以异步的方式来执行吧!多线程我们首先想到的是new System.Threading.Thread对象来实现,但我们这里只是期望一个不怎么“重要”的线程,或

者说不频繁使用,或者是仅仅执行一个操作(可以理解为一个事件),那么可以采用System.ComponentModel.BackgroundWorker来做更贴切。

看看他的用法:

1. 首先new 一个实例: private System.ComponentModel.BackgroundWorker backgroundGetData = new System.ComponentModel.BackgroundWorker();

2.定义一个后台进程执行任务的事件,名称为 BackgroundGetData_DoWork,类型是DoWorkEventHandler . 代码如下:

private void BackgroundGetData_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)

{

XDocument doc = XDocument.Load(url);

//很容易从这个doc里面获取天气,股票信息了,因为XDocument能够把一个url例如http://www.nasdaq.com/aspxcontent/NasdaqRSS.aspx?data=quotes&symbol=CAC.

//怎么处理这个xml来获取这些信息就不在这里写了。只需记住给System.Xml.Linq.XDocument传入一个正确的url即可。

}

3.注册这个事件: this.backgroundGetData.DoWork += new System.ComponentModel.DoWorkEventHandler(this.BackgroundGetData_DoWork);

4.倒数第二步是异步调用这个后台进程.

很简单,即使是不同进程间调用,只要知道这个后台进程是定义在主线程里的,所以只需知道这个后台对象的实例,你就可以执行它里面的方法:

/// <summary>
/// Retrieve the data on a separate thread. Async call.
/// </summary>
public void GetOnlineData()
{
System.Diagnostics.Trace.WriteLine("GetOnlineData(): Retrieving new data for the scroll queue.");
if (!this.backgroundGetData.IsBusy)
{
this.backgroundGetData.RunWorkerAsync();
}
}

5.最后就是不要忘记自己写代码销毁它。怎么销毁呢?定义了后台进程的这个类实现这个IDisposable这个接口,在Dispose()方法里写代码就好了。

public void Dispose()
{
if (this.backgroundGetData != null)
{
this.backgroundGetData.Dispose();
this.backgroundGetData = null;
}
}

最后,补充一句,虽然System.ComponentModel.BackgroundWorker提供了异步执行的机制,但是后台进程如果频繁执行异步,比如每10秒执行一次,由于我的主程序是个使用WPF做效果的winform程序,后台的异步操作会影响到我主程序界面的执行效果,比如部分界面元素丢失,而且感觉程序执行效果会很卡。

private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.timer2 = new System.Windows.Forms.Timer(this.components); this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); this.backgroundWorker2 = new System.ComponentModel.BackgroundWorker(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.Location = new System.Drawing.Point(25, 28); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(55, 16); this.label1.TabIndex = 0; this.label1.Text = "站名:"; // // label2 // this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label2.Location = new System.Drawing.Point(25, 73); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(55, 16); this.label2.TabIndex = 1; this.label2.Text = "蒸发:"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(88, 30); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 12); this.label3.TabIndex = 2; this.label3.Text = "label3"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(90, 75); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(41, 12); this.label4.TabIndex = 3; this.label4.Text = "label4"; // // timer1 // this.timer1.Enabled = true; this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // timer2 // this.timer2.Interval = 1000; this.timer2.Tick += new System.EventHandler(this.timer2_Tick); // // backgroundWorker1 // this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork); // // backgroundWorker2 // //this.backgroundWorker2.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker2_DoWork); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(406, 114); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "数据传输"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Timer timer2; private System.ComponentModel.BackgroundWorker backgroundWorker1; private System.ComponentModel.BackgroundWorker backgroundWorker2; } }这部分该怎么修改对应刚刚提取数据的修改
最新发布
10-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值