Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:44:17+00:00 2026-06-17T04:44:17+00:00

Here is my approach. I have several IEventProviders , interface IEventProvider { Task<Event> GetEvent();

  • 0

Here is my approach. I have several IEventProviders,

interface IEventProvider
{
    Task<Event> GetEvent();
}

Then I got a container class to wrap them, and keep calling and awaiting the GetEvent() to wait for next Event, e.g. socket async receiving, timer ticks, etc.

class EventProviderContainer : IEventProvider
{
    private IEventProvider[] _providers;
    private Task<Event>[] _tasks;

    public EventProviderContainer(params IEventProvider[] providers)
    {
        _providers = providers;
    }

    public async Task<Event> GetEvent()
    {
        // Fill the _tasks first time we call the method.
        if (_tasks == null)
            _tasks = (from p in _providers select p.GetEvent()).ToArray();

        Task<Event> task = await Task<Event>.WhenAny(_tasks);

        // Get the provider index whose previous task is done.
        int index = Array.IndexOf(_tasks, task);
        // put next event of the provider into array.
        _tasks[index] = _providers[index].GetEvent();

        return await task;
    }
}

I think it is a bit of ugly. Is it a better way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:44:18+00:00Added an answer on June 17, 2026 at 4:44 am

    If you want one event for each provider at a time then I recommend you check out the Processing tasks as they complete MSDN article which includes an Interleaved method. This method takes a collection of tasks and returns a new array of tasks that will yield in order of completion.

    On the other hand, if you want to continuously receive events from each provider as they arrive, then I recommend you look at the Reactive Extensions (Rx) project from Microsoft.

    Using Rx, your event provider interface would become something like:

    public interface IEventProvider
    {
        IObservable<Event> OnEvent();
    }
    

    Your container provider would then use the Observable.Merge extension method to combine the events of each child provider.

    return _providers.Select(provider => provider.OnEvent()).Merge();
    

    To actually receive the events, you subscribe to the observable by attaching a callback delegate that’s executed each time a new event is available.

    var provider = new EventProviderContainer(
        new TestEventProvider("a", 1000),
        new TestEventProvider("b", 1300),
        new TestEventProvider("c", 1600));
    provider.OnEvent().Subscribe(Console.WriteLine);
    Console.ReadLine();
    

    The above example uses a test event provider that returns a continuous stream of events at the given period in milliseconds using the Observable.Timer extension method.

    return Observable.Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(_period))
                     .Select(i => new TestEvent(_name, i));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Django newbie here, I have several types of models, in each of them the
I have read several interesting C#/F# comparisons here on stackoverflow. So, first of all,
I have several methods that execute the same setup code and then some cleanup
I have seen several posts here about cocos2d-android, so ambition to get more idea
Let's suppose I have an interface named Controller. Several classes implement this interface and
I have been through several SO questions regarding this and my approach is a
I'm looking at the best practice approach here. I have a web page that
Here's my aproach: I have a custom ListView containing a custom Adapter containing two
Here is a live example: http://jsfiddle.net/Pzvdv/ <ul id=navigation> <li><a href=#>HOME</a></li> <li><a href=#>OUR APPROACH</a></li> <li><a
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.