我有一个 .NET 应用程序,它在批量导入中处理大约 300,000 条记录,每条记录需要几秒钟,所以我想并行化它。ProcessWithAnsycDelegates()
在下面的代码中,和有什么区别ProcessWithThreadPool()
?
public class ResultNotification
{ public EventHandler event Success;
public EventHandler event Fail;
internal void Notify(bool sucess) {if (success) Success(); else Fail();}
}
public static class Processor
{ public ResultNotification ProcessWithAnsycDelegates(Record record)
{ var r = new ResultNotification();
Func<Record,bool> processRecord=new RecordProcessor().ProcessRecord;
processRecord.BeginInvoke
( record
,ar => result.Notify(processRecord.EndInvoke(ar))
,null);
return r;
}
public ResultNotification ProcessWithThreadPool(Record r)
{ var r = new ResultNotification();
var rp = new RecordProcessor();
ThreadPool.QueueWorkUserItem(_=>result.Notify(rp.ProcessRecord(r)));
return r;
}
}