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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:47:10+00:00 2026-05-22T15:47:10+00:00

I want to double check with others whether this would be the correct way

  • 0

I want to double check with others whether this would be the correct way to create an extension method that begins an asynchronous process, and returns a function that when invoked essentially waits on that process and gets the result.

    public static Func<R> HandleInvoke<T, R>(this Func<T, R> function, T arg, Action<IAsyncResult> callback)
    {
        IAsyncResult result = function.BeginInvoke(arg, new AsyncCallback(callback), function);

        return delegate
        {
            return function.EndInvoke(result);
        };
    }

Essentially I want to use it like such (pseudo code):

Func<R> myFunc = (some delegate).HandleInvoke(arg, callback);  
                     // at this point the operation begins, but will be nonblocking

                     // do other stuff

var result = myFunc();   // now I am deciding to wait on the result, which is blocking

Wasnt sure if I need to worry about waiting on WaitHandles in this situation or not. Also not sure if passing in a callback would even be necessary. Also I think this constitutes a closure?

EDIT

Ended up with this,

    public static Func<R> HandleInvoke<T, R>(this Func<T, R> function, T arg)
    {
        IAsyncResult asyncResult = function.BeginInvoke(arg, iAsyncResult =>
            {
                if (!(iAsyncResult as AsyncResult).EndInvokeCalled)
                {
                    (iAsyncResult.AsyncState as Func<T, R>).EndInvoke(iAsyncResult);
                }

            }, function); 

        return delegate
        {
            WaitHandle.WaitAll(new WaitHandle[] { asyncResult.AsyncWaitHandle }); 

            return function.EndInvoke(asyncResult); 
        };
    }

Which seems to work well. The callback checks if EndInvoke has been called, and if not, calls it. Otherwise EndInvoke is called within the returned delegate.

2ND EDIT

Here is my latest attempt — hasnt thrown any errors at me yet and seems to handle it well. I couldn’t get it to work where the delegate returned the function.EndInvoke() result, but the delegate waits until EndInvoke has been called in the anonymous callback before returning R. Thread.Sleep() probably isnt the best solution, though. Also could use more checking to make sure that R was actually assigned to in each case.

    public static Func<R> HandleInvoke<T, R>(this Func<T, R> function, T arg)
    {
        R r = default(R); 

        IAsyncResult asyncResult = function.BeginInvoke(arg, result =>
            {
                r = (result.AsyncState as Func<T, R>).EndInvoke(result);

            }, function); 


        return delegate
        {
            while (!(asyncResult as AsyncResult).EndInvokeCalled)
            {
                Thread.Sleep(1); 
            }

            return r; 
        };
    }
  • 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-05-22T15:47:11+00:00Added an answer on May 22, 2026 at 3:47 pm

    This should work but I’m not keen on the design… Here is the basic problem.

    If myFunc is called than you should not call EndInvoke in the callback but if myFunc is not called, because you don’t care about the return value then EndInvoke must be called in the callback. This makes using the API non-obvious and error prone.

    with the sleep you have in there it is racy, though it isn’t likely to bite you very often. This uses proper synchronization primitives to guarantee that everything will happen in the right order. This is untested code, but should work

        public static Func<R> HandleInvoke<T, R>(this Func<T, R> function, T arg)
        {
            R retv = default(R);
            bool completed = false;
    
            object sync = new object();
    
            IAsyncResult asyncResult = function.BeginInvoke(arg, 
                iAsyncResult =>
                {
                    lock(sync)
                    {
                        completed = true;
                        retv = function.EndInvoke(iAsyncResult);
                        Monitor.Pulse(sync); // wake a waiting thread is there is one
                    }
                }
                , null);
    
            return delegate
            {
    
                lock (sync)
                {
                    if (!completed) // if not called before the callback completed
                    {
                        Monitor.Wait(sync); // wait for it to pulse the sync object
                    }
                    return retv;
                }
            };
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably this is just the way Rails works, but I want to double-check because
This is probably a naive question - but I want to double check to
I'm pretty sure this isn't possible, but just want to double check. Obviously the
I have some double values I want to convert to a string with this
How would I check if the input is really a double? double x; while
Can someone double-check my trimmed-down example? When the Documents table is updated, I want
I want to make functions Double -> Double an instance of the Num typeclass.
I want to divide a number of type double, by an int. I only
I want to output the value of a double in it's full precision. However,
I want to listen both click and double click events for advanced data grid

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.