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

  • SEARCH
  • Home
  • 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 7988943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:36:21+00:00 2026-06-04T12:36:21+00:00

just trying to get my head around Rx I am using Rx to poll

  • 0

just trying to get my head around Rx

I am using Rx to poll a website every 2 seconds

var results = new List<MyDTO>();
var cx = new WebserviceAPI( ... );
var callback = cx.GetDataAsync().Subscribe(rs => { results.AddRange(rs); });
var poller = Observable.Interval(TimeSpan.FromSeconds(2)).Subscribe( _ => { cx.StartGetDataAsync(); });

(The webservice API exposes a getItemsAsync/getItemsCompleted event handler type mechanism from which I am creating an observable).

When the web site returns, I am unpacking the “business part of” the response into an IEnumerable of DTOs

public IObservable<IEnumerable<MyDTO>> GetDataAsync()
{
    var o = Observable.FromEventPattern<getItemsCompletedEventHandler,getItemsCompletedEventArgs>(
        h => _webService.getItemsCompleted += h,
        h => _webService.getItemsCompleted -= h);

    return o.Select(c=> from itm in c.EventArgs.Result.ItemList
                        select new MyDTO()
                        {
                           ...
                        });
}

My reasoning being that given that all the data was just there in the string, it made sense just to pack it up there an then into an IEnumerable … but now I’m not sure if that is right!

If the website takes longer than 2 secs to respond I am finding that MSTest is crashing out. When debugging, the error being generated is

“There was an error during asynchronous processing. Unique state
object is required for multiple asynchronous simultaneous operations
to be outstanding”

with the inner exception

“Item has already been added. Key in dictionary: ‘System.Object’ Key
being added: ‘System.Object'”

I am supposing that the problem is one of reentrancy in that the next call is starting and returning data before the previous call has finished populating the data.

So I’m not sure whether

  1. I have put the thing together quite right
  2. I should be throttling the connection in some way so as to avoid re-entrancy.
  3. I should use a different intermediate data structure (or mechanism)
    instead of an IEnumerable

I would appreciate some guidance.

EDIT 1:
So I have changed the web call to include a unique state object

public void StartGetDataAsync()
{
    ...
    //  was: _webService.getItemsAsync(request);
    _webService.getItemsAsync(request, Guid.NewGuid());
}

and made it work. But I am still unsure if that is the right way to do it

EDIT 2 – Web service sigs:
I’m consuming a soap web service which the webServiceApi class wraps. The references.cs created contains the following methods

public void getItemsAsync(GetItemsReq request, object userState) 
{
    if ((this.getItemsOperationCompleted == null)) 
    {
        this.getItemsOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetItemsOperationCompleted);
    }
    this.InvokeAsync("getItems", new object[] {
                    request}, this.getItemsOperationCompleted, userState);
}

private System.Threading.SendOrPostCallback getItemsOperationCompleted;

public event getItemsCompletedEventHandler getItemsCompleted;

public delegate void getItemsCompletedEventHandler(object sender, getItemsCompletedEventArgs e);

public partial class getItemsCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs 
{
    ...
}

private void OngetItemsOperationCompleted(object arg) 
{
    if ((this.getItemsCompleted != null)) 
    {
        System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
        this.getItemsCompleted(this, new getItemsCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
    }
 }

Probably given you too much (or missed something)!

Thx

  • 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-04T12:36:22+00:00Added an answer on June 4, 2026 at 12:36 pm

    I think I’ve got a decent starting point for you.

    Basically I think you need to abstract away the complexity of the web service and create a nice clean function to get your results.

    Try something like this:

    Func<GetItemsReq, IObservable<getItemsCompletedEventArgs>> fetch =
        rq =>
            Observable.Create<getItemsCompletedEventArgs>(o =>
            {
                var cx = new WebserviceAPI(/* ... */);
                var state = new object();
                var res =
                    Observable
                        .FromEventPattern<
                            getItemsCompletedEventHandler,
                            getItemsCompletedEventArgs>(
                            h => cx.getItemsCompleted += h,
                            h => cx.getItemsCompleted -= h)
                        .Where(x => x.EventArgs.UserState == state)
                        .Take(1)
                        .Select(x => x.EventArgs);
                var subscription = res.Subscribe(o);
                cx.getItemsAsync(rq, state);
                return subscription;
            });
    

    Personally I would go one step further and define a return type, say GetItemsReq, that doesn’t include the user state object, but is basically the same as getItemsCompletedEventArgs.

    You should then be able to use Observable.Interval to create the polling that you need.

    If your web service implements IDisposable then you should add an Observable.Using call into the above function to correctly dispose of the web service when it is complete.

    Let me know if this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started using log4net and trying to get my head around the config and
Just trying to get my head around what can happen when things go wrong
I am just trying to get my head around various pointer concepts and I
I am just trying to get my head around simple view switching for the
Just trying to still get my head around IOC principles. Q1: Static Methods -
I'm new to nHibernate, and trying to get my head around the proper way
I'm still trying to get my head around using Java's generics. I have no
I am just trying to get my head around SSL. I have set up
I was just reading the guidelines and trying to get my head around the
I'm new to Doctrine, and I'm trying to get my head around both Doctrine

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.