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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:01:24+00:00 2026-05-13T18:01:24+00:00

We have this common scenario where we have a method that performs some action

  • 0

We have this common scenario where we have a method that performs some action asyncronously and raises an event when it’s done.

There are times where we want it done synchronously instead so we have code that looks similar to this:

ManualResetEvent reset = new ManualResetEvent(false);
someobject.AsyncActionDone += (sender, args) => reset.Set();
someobject.PerformAsyncAction();
reset.WaitOne();

Is there a way to write a helper method to do this? I can pass in the Action to perform, but I’m not sure how to pass in something that lets the helper method know which event to listen to since it doesn’t look like you can pass in an EventHandler as a parameter.

Preferably a solution that doesn’t require reflection

There seems to be some confusion, this is a sample of what someobject’s class is like:

public class SomeClass
{
    private ExternalServer someServerOverTheNetwork = new ExternalServer();

    public event EventHandler AsyncActionDone;
    public Data SomeData { get; set; }
    public void PerformAsyncAction()
    {
        someServerOverTheNetwork.GetSomeData(OnDataRetrived);
    }

    public Data OnDataRetrived(Data someData)
    {
        AsyncActionDone(this, new DataEventArgs(someData));
    }
}
  • 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-13T18:01:24+00:00Added an answer on May 13, 2026 at 6:01 pm

    I would consider implementing the Asynchronous Design Pattern in the objects that performs asynchronous operation.

    public object Operation(object arg)
    {
        var ar = BeginOperation(arg, null, null);
    
        return EndOperation(ar);
    }
    
    public IAsyncResult BeginOperation(object arg, AsyncCallback asyncCallback, object state)
    {
        AsyncResult asyncResult = new AsyncResult(asyncCallback, state);
    
        // Lauch the asynchronous operation
    
        return asyncResult;
    }
    
    private void LaunchOperation(AsyncResult asyncResult)
    {
        // Do something asynchronously and call OnOperationFinished when finished
    }
    
    private void OnOperationFinished(AsyncResult asyncResult, object result)
    {
        asyncResult.Complete(result);
    }
    
    
    public object EndOperation(IAsyncResult asyncResult)
    {
        AsyncResult ar = (AsyncResult)asyncResult;
    
        return ar.EndInvoke();
    }
    

    With this pattern you have the flexibility of having multiple concurrent asynchronous operation on your object.

    Note: You can easily find an implementation of a generic AsyncResult class on the web.

    Edit:

    As you want to keep the current design, if all your object can only have one asynchronous operation, then you could define an IAsyncOperation interface and implement it in all your object.

    public interface IAsyncOperation
    {
        event EventHandler AsyncActionDone;
        void PerformAsyncAction();
    }
    

    Then you could have:

    public static CallSynchronously(IAsyncOperation asyncOperation)
    {
        ManualResetEvent reset = new ManualResetEvent(false);
        asyncOperation.AsyncActionDone += (sender, args) => reset.Set();
        asyncOperation.PerformAsyncAction();
        reset.WaitOne();
    }
    

    If your objects can contain multiple asynchronous operation, then without reflection I think there is no way to achieve what you want to do, but you could still define a synchronous version of all asynchronous operation that wraps the ManualResetEvent.

    public void PerformAction()
    {
        ManualResetEvent reset = new ManualResetEvent(false);
        this.AsyncActionDone += (sender, args) => reset.Set();
        this.PerformAsyncAction();
        reset.WaitOne();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • 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 I would recommend that your URL be http://localhost:1985/Materials/Details/2/Steel This seems… May 14, 2026 at 8:09 pm
  • Editorial Team
    Editorial Team added an answer I'm the OP. MSYS is the exact thing what I… May 14, 2026 at 8:09 pm
  • Editorial Team
    Editorial Team added an answer Not really. This is normally done using javascript. there is… May 14, 2026 at 8:09 pm

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.