使用 TPL 实现经典异步模式
我正在尝试为 WF 4 实现自定义 TrackingParticipant。我可以编写 Track 方法,但我的实现会很慢。
如何使用 .NET 4.0 的任务并行库 (TPL) 实现 Begin/EndTrack 覆盖?我看过 TPL 和传统 .NET 异步编程,但我没有确定如何在这里应用它。
请注意,TrackingParticipant 是 .NET 的一部分,并具有使用虚拟方法预定义的经典异步模式。
public class MyTrackingParticipant : TrackingParticipant
{
protected override IAsyncResult BeginTrack(
TrackingRecord record, TimeSpan timeout,
AsyncCallback callback, object state)
{
// ?
}
protected override void EndTrack(IAsyncResult result)
{
// ?
}
protected override void Track(TrackingRecord record, TimeSpan timeout)
{
// synchronous code to be called
}
}
I'm trying to implement a custom TrackingParticipant for WF 4. I can write the Track method, but my implementation will be slow.
How can I implement the Begin/EndTrack overrides using .NET 4.0's Task Parallel Library (TPL)? I've looked at TPL and Traditional .NET Asynchronous Programming but am not sure how to apply it here.
Note that TrackingParticipant is part of .NET and has the Classic Async Pattern predefined using virtual methods.
public class MyTrackingParticipant : TrackingParticipant
{
protected override IAsyncResult BeginTrack(
TrackingRecord record, TimeSpan timeout,
AsyncCallback callback, object state)
{
// ?
}
protected override void EndTrack(IAsyncResult result)
{
// ?
}
protected override void Track(TrackingRecord record, TimeSpan timeout)
{
// synchronous code to be called
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是实现经典 APM 编程模型的通用模式:
如果 EndXXX 方法返回结果,您实际上将返回
Task
的Result
属性,而不是仅仅调用等等
。例如:This is the generic pattern with which to implement the classic APM programming model:
If the EndXXX method returned a result you would actually return the
Result
property of theTask
instead of just callingWait
. For example: