实现使用TPL经典异步模式模式、经典、TPL

2023-09-08 08:58:50 作者:忘了我、就亡了我

我想实现一个自定义TrackingParticipant为WF 4.我可以写的跟踪方法,但我的实现将是缓慢的。

我怎样才能实现开始/ EndTrack取代使用.NET 4.0的任务并行库(TPL)?我看着 TPL和传统.NET异步编程但我不知道如何运用它在这里。

需要注意的是TrackingParticipant是.NET的一部分,拥有经典的异步模式predefined使用虚拟方法。

 公共类MyTrackingParticipant:TrackingParticipant
{
    保护覆盖IAsyncResult的BeginTrack(
        TrackingRecord纪录,时间跨度超时,
        AsyncCallback的回调,对象的状态)
    {
        //?
    }

    保护覆盖无效EndTrack(IAsyncResult的结果)
    {
        //?
    }

    保护覆盖无效轨道(TrackingRecord记录,时间跨度超时)
    {
        //同步code被称为
    }
}
 

解决方案

这是它实现了经典的APM编程模型的通用模式:

 保护覆盖的IAsyncResult BeginTrack(TrackingRecord记录,时间跨度超时,AsyncCallback的回调,对象的状态)
{
    工作的结果= Task.Factory.StartNew(
        (taskState)=>
        {
           // ...在这里您异步工作...
        },
        州);

    如果(回调!= NULL)
    {
        result.ContinueWith((吨)=>回调(吨));
    }

    返回结果;
}

保护覆盖无效EndTrack(IAsyncResult的asyncResult)
{
   //呼叫等待阻塞,直到任务完成和/或导致发生的任何异常传播到主叫方
   ((任务)asyncResult).Wait();
}
 
分布式消息队列详解 10min搞懂同步和异步架构

如果该EndXXX方法返回一个结果,你实际上返回结果工作的属性,而不是只调用等待。例如:

 保护覆盖INT EndAwesomeCalculation(IAsyncResult的asyncResult)
{
   //这将阻止,直到结果是可用的和/或导致发生传播给调用者的任何异常
   返程((任务< INT>)asyncResult)。结果;
}
 

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
    }
}

解决方案

This is the generic pattern with which to implement the classic APM programming model:

protected override IAsyncResult BeginTrack(TrackingRecord record, TimeSpan timeout, AsyncCallback callback, object state)
{
    Task result = Task.Factory.StartNew(
        (taskState) =>
        {
           // ... your async work here ...
        },
        state);

    if(callback != null)
    {
        result.ContinueWith((t) => callback(t));
    }

    return result;
}

protected override void EndTrack(IAsyncResult asyncResult)
{
   // Call wait to block until task is complete and/or cause any exceptions that occurred to propagate to the caller
   ((Task)asyncResult).Wait();
}

If the EndXXX method returned a result you would actually return the Result property of the Task instead of just calling Wait. For example:

protected override int EndAwesomeCalculation(IAsyncResult asyncResult)
{
   // This will block until the result is available and/or cause any exceptions that occurred propagate to the caller
   return ((Task<int>)asyncResult).Result;
}