Task Parallel Library - Custom Task Schedulers

后端 未结 4 598
萌比男神i
萌比男神i 2021-02-06 12:35

I have a requirement to fire off web service requests to an online api and I thought that Parallel Extensions would be a good fit for my needs.

The web service in questi

4条回答
  •  执笔经年
    2021-02-06 13:13

    If you need to throttle by time, you should check out Quartz.net. It can facilitate consistent polling. If you care about all requests, you should consider using some sort of queueing mechanism. MSMQ is probably the right solution but there are many specific implementations if you want to go bigger and use an ESB like NServiceBus or RabbitMQ.

    Update:

    In that case, TPL Dataflow is your preferred solution if you can leverage the CTP. A throttled BufferBlock is the solution.

    This example comes from the documentation provided by Microsoft:

    // Hand-off through a bounded BufferBlock
    private static BufferBlock m_buffer = new BufferBlock(
        new DataflowBlockOptions { BoundedCapacity = 10 });
    
    // Producer
    private static async void Producer()
    {
        while(true)
        {
            await m_buffer.SendAsync(Produce());
        }
    }
    
    // Consumer
    private static async Task Consumer()
    {
        while(true)
        {
            Process(await m_buffer.ReceiveAsync());
        }
    }
    
    // Start the Producer and Consumer
    private static async Task Run()
    {
        await Task.WhenAll(Producer(), Consumer());
    }
    

    Update:

    Check out RX's Observable.Throttle.

提交回复
热议问题