您是否尝试过响应式扩展?
http://msdn.microsoft.com/en-us/data/gg577609.aspx
Rx 是 Microsoft 的一项新技术,其重点如官方网站所述:
Reactive Extensions (Rx)... ...是一个使用可观察集合和 LINQ 样式查询运算符组成异步和基于事件的程序的库。
您可以将其作为 Nuget 包下载
https://nuget.org/packages/Rx-Main/1.0.11226
由于我目前正在学习 Rx,所以我想举这个例子,只是为它编写代码,我最终得到的代码实际上并不是并行执行的,而是完全异步的,并且保证了源代码行是按顺序执行的。
也许这不是最好的实现,但就像我说的我正在学习 Rx,(线程安全应该是一个很好的改进)
这是我用来从后台线程返回数据的 DTO
class MyItem
{
public string Line { get; set; }
public int CurrentThread { get; set; }
}
这些是做实际工作的基本方法,我用一个简单的方法来模拟时间,Thread.Sleep
我正在返回用于执行每个方法的线程Thread.CurrentThread.ManagedThreadId
。注意它的定时器ProcessLine
是4秒,这是最耗时的操作
private IEnumerable<MyItem> ReadLinesFromFile(string fileName)
{
var source = from e in Enumerable.Range(1, 10)
let v = e.ToString()
select v;
foreach (var item in source)
{
Thread.Sleep(1000);
yield return new MyItem { CurrentThread = Thread.CurrentThread.ManagedThreadId, Line = item };
}
}
private MyItem UpdateResultToDatabase(string processedLine)
{
Thread.Sleep(700);
return new MyItem { Line = "s" + processedLine, CurrentThread = Thread.CurrentThread.ManagedThreadId };
}
private MyItem ProcessLine(string line)
{
Thread.Sleep(4000);
return new MyItem { Line = "p" + line, CurrentThread = Thread.CurrentThread.ManagedThreadId };
}
以下方法我使用它只是为了更新 UI
private void DisplayResults(MyItem myItem, Color color, string message)
{
this.listView1.Items.Add(
new ListViewItem(
new[]
{
message,
myItem.Line ,
myItem.CurrentThread.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString()
}
)
{
ForeColor = color
}
);
}
最后这是调用 Rx API 的方法
private void PlayWithRx()
{
// we init the observavble with the lines read from the file
var source = this.ReadLinesFromFile("some file").ToObservable(Scheduler.TaskPool);
source.ObserveOn(this).Subscribe(x =>
{
// for each line read, we update the UI
this.DisplayResults(x, Color.Red, "Read");
// for each line read, we subscribe the line to the ProcessLine method
var process = Observable.Start(() => this.ProcessLine(x.Line), Scheduler.TaskPool)
.ObserveOn(this).Subscribe(c =>
{
// for each line processed, we update the UI
this.DisplayResults(c, Color.Blue, "Processed");
// for each line processed we subscribe to the final process the UpdateResultToDatabase method
// finally, we update the UI when the line processed has been saved to the database
var persist = Observable.Start(() => this.UpdateResultToDatabase(c.Line), Scheduler.TaskPool)
.ObserveOn(this).Subscribe(z => this.DisplayResults(z, Color.Black, "Saved"));
});
});
}
此过程完全在后台运行,这是生成的输出: