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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:53:46+00:00 2026-05-10T16:53:46+00:00

When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something

  • 0

When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something like the following (and to save you guessing this is an async wrapper around a cache):

IAsyncResult BeginPut(string key, object value) {     Action<string, object> put = this.cache.Put;     return put.BeginInvoke(key, value, null, null); }  void EndPut(IAsyncResult asyncResult) {     var put = (Action<string, object>)((AsyncResult)asyncResult).AsyncDelegate;     put.EndInvoke(asyncResult); } 

This works perfectly well because it’s known what the type of delegate is, so it can be cast. However it starts to get messy when you have two Put methods, because although the method returns void you seemingly have to cast it to a strongly typed delegate to end the invocation, e.g.

IAsyncResult BeginPut(string key, object value) {     Action<string, object> put = this.cache.Put;     return put.BeginInvoke(key, value, null, null); }  IAsyncResult BeginPut(string region, string key, object value) {     Action<string, string, object> put = this.cache.Put;     return put.BeginInvoke(region, key, value, null, null); }  void EndPut(IAsyncResult asyncResult) {     var put = ((AsyncResult)asyncResult).AsyncDelegate;      var put1 = put as Action<string, object>;     if (put1 != null)      {         put1.EndInvoke(asyncResult);         return;     }      var put2 = put as Action<string, string, object>;     if (put2 != null)      {         put2.EndInvoke(asyncResult);         return;     }      throw new ArgumentException('Invalid async result', 'asyncResult'); } 

I’m hoping there is a cleaner way to do this, because the only thing I care about the delegate is the return type (in this case void) and not the arguments that were supplied to it. But I’ve racked my brains and asked others in the office, and nobody can think of the answer.

I know one solution is to write a custom IAsyncResult, but that’s such a difficult task with the potential threading issues around things like lazy instantiation of the WaitHandle that I’d rather have this slightly hacky looking code than go down that route.

Any ideas on how to end the invocation without a cascading set of is checks?

  • 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. 2026-05-10T16:53:47+00:00Added an answer on May 10, 2026 at 4:53 pm

    I was wrong, there is a cleaner way.

    You create Action( IAsyncResult ) delegates for the specific EndInvoke() method in the same context where you already know the specific type of the delegate, passing it as the AsyncState. I’m passing EndPut() as the callback for convenience.

    IAsyncResult BeginPut( string key, object value ) {     Action<string, object> put = this.Put;     return put.BeginInvoke( key, value, EndPut,         new Action<IAsyncResult>( put.EndInvoke ) ); }  IAsyncResult BeginPut( string region, string key, object value ) {     Action<string, string, object> put = this.Put;     return put.BeginInvoke( region, key, value, EndPut,         new Action<IAsyncResult>( put.EndInvoke ) ); } 

    And then you finish it off.

    void EndPut( IAsyncResult asyncResult ) {     var del = asyncResult.AsyncState as Action<IAsyncResult>;     del( asyncResult ); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I'm writing a class library that will offer some asynchronous processing, and want to
I'm writing some browser side dynamic functionality and using HTTP Basic Auth to protect
I am trying to create a new XML file from an exisiting one using
Can you make file copying faster through multiple threading? Edit : To clarify, suppose

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.