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 8246241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:26:32+00:00 2026-06-07T22:26:32+00:00

I have an API whose interface looks like this: void SendRequest(Guid id, IRequest request);

  • 0

I have an API whose interface looks like this:

void SendRequest(Guid id, IRequest request);
event EventHandler<ResponseEventArgs> ResponseReceived;

What’s the best way of implementing this method?

Task<T> GetResponse(IRequest request) where T: IRequest

Note that multiple requests may overlap each other, so when a response comes back I need to look up the parent request. I have a feeling TaskCompletionSource may be of use, but can’t quite piece it together.

  • 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-07T22:26:33+00:00Added an answer on June 7, 2026 at 10:26 pm

    EDIT:

    If you want to do this without blocking a thread, you can do something like this using TaskCompletionSource<T>:

    var completionSource = new TaskCompletionSource<T>();
    
    var requestIdentifier = Guid.NewGuid();
    
    EventHandler<ResponseEventArgs> handler = null;
    
    handler = (sender, args) =>
    {
        if(args.RequestIdentifier == requestIdentifier)
        {
            api.ResponseReceived -= handler; 
    
            // TrySetResult avoids re-entrancy problems in case of an
            // API that sends duplicates, but there other ways of 
            // dealing with this too.
            completionSource.TrySetResult((T)args.Response);
        }      
    };
    
    
    api.ResponseReceived += handler; 
    
    // Make this async if you want.
    api.SendRequest(requestIdentifier, request); 
    
    return completionSource.Task;
    

    Original Answer:

    I think you want something like the following, which uses a ManualResetEvent to block the thread until the event is raised by the API:

    return Task.Factory.StartNew<T>(() =>
    {
        var waitHandle = new ManualResetEvent(false);
    
        T result = default(T);
    
        var requestIdentifier = Guid.NewGuid();
    
        EventHandler<ResponseEventArgs> handler = (sender, args) =>
        {
             if(args.RequestIdentifier == requestIdentifier)
             {
                 result = (T)args.Response; // Not sure how this looks in your API
                 waitHandle.Set(); // Unblock the thread running the task     
             }      
        };
    
        // Attach handler to respond to the response being received.
        api.ResponseReceived += handler; 
    
        // Send request off.
        api.SendRequest(requestIdentifier, request); 
    
        // Wait until response is received.
        waitHandle.WaitOne(); 
    
        // Detach handler to prevent leak.
        api.ResponseReceived -= handler; 
    
        return result;
    });
    

    For a cleaner way of doing this, look into Reactive Extensions.

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

Sidebar

Related Questions

I have an API in the form of a JAR that I would like
I have scoured the internet for this answer, and I feel like I keep
This might sound like a duplicate, but I have searched through the forum questions
I have java API which return this type: ArrayList[ArrayList[String]] = Foo.someJavaMethod() In scala program,
If I have a page whose url alias is /api/user/create, how can I name
I have a django application which is exposing a API whose url conf look
I have an NSData that I would like to read as an NSInputStream. This
I see many Java packages have api, impl and bundle jars (name-api.jar, name-impl.jar, name-bundle.jar).
I have an api which takes uni code data as c character array and
We have an API built on Rails, and processing some stuff with resque. After

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.