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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:13:11+00:00 2026-06-02T10:13:11+00:00

I have a console app that executes a class method (in a different project).

  • 0

I have a console app that executes a class method (in a different project).
This method performs a POST to a webservice asynchronously (this is a lengthy operations of 3 minutes) and has a callback that returns the number of records affected by the POST. In the meantime the program continues doing other things that takes less than 3 minutes and the program exits before the callback returns.

In the past I have used a ManualResetEvent as depicted below to prevent completion but in this case my method is in a different class library that I would like to keep as clean as possible.


static ManualResetEvent resetEvent = new ManualResetEvent(false)

static void Main()
{
     CallAsyncMethod();
     // Do other things...
     resetEvent.WaitOne(); // Blocks until "set"
}

void AsyncMethodCallback()
{
     // Do processing on completion...
     resetEvent.Set(); // Allow the program to exit
}

I would appreciate any help in coming up with a clean pattern to accomplish this without polluting the called class with execution flags.

  • 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-02T10:13:17+00:00Added an answer on June 2, 2026 at 10:13 am

    If you can (in particular, if you’re using .NET 4), make your CallAsyncMethod return a Task or Task<T>. Then you can easily attach a continuation, wait for it to complete etc. If the only purpose of the callback is to get the number of records, returning a Task<int> would be ideal. You can use TaskCompletionSource<TResult> so populate the result really easily within the async method, so you don’t need the callback at all within your main class, which would have a main method something like:

    static void Main()
    {
         Task<int> asyncTask = CallAsyncMethod();
         // Do other things...
         int count = asyncTask.Result;
         // Do anything you need with the count
    }
    

    The new async features in C# 5 are designed to work well with Task<T>, so you’ll be all ready to embrace the new version when it arrives.

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

Sidebar

Related Questions

I have a console app that performs a lengthy process. I am printing out
I have a windows service that executes a console app every hour and it's
i have a method that creates a Process calling a console app. double myProcess()
I have one console app that is doing some lengthy syncing to an ftp
I have console app A that references another console app B. B app has
I have a console app that's geared to be automatically ran as a Scheduled
I have a console command line app that use NHibernate . I am trying
Help me, Stackoverflow! I have a simple .NET 3.5 console app that reads some
I have a console app built on .NET 4 that uses the HttpClient library
I have the following code that allows my console app to go to the

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.