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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:46:16+00:00 2026-05-12T05:46:16+00:00

What is the use of AsyncCallback and why should we use it?

  • 0

What is the use of AsyncCallback and why should we use it?

  • 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-12T05:46:16+00:00Added an answer on May 12, 2026 at 5:46 am

    When the async method finish the processing, AsyncCallback method is automatically called, where post processing statements can be executed. With this technique there is no need to poll or wait for the async thread to complete.

    Here’s some more explanation on Async Callback usage:

    Callback Model: The callback model requires that we specify a method to callback on and include any state that we need in the callback method to complete the call. The callback model can be seen in the following example:

    static byte[] buffer = new byte[100];
    
    static void TestCallbackAPM()
    {
        string filename = System.IO.Path.Combine (System.Environment.CurrentDirectory, "mfc71.pdb");
    
        FileStream strm = new FileStream(filename,
            FileMode.Open, FileAccess.Read, FileShare.Read, 1024,
            FileOptions.Asynchronous);
    
        // Make the asynchronous call
        IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length,
            new AsyncCallback(CompleteRead), strm);
    }
    

    In this model, we are creating a new AsyncCallback delegate, specifying a method to call (on another thread) when the operation is complete. Additionally, we are specifying some object that we might need as the state of the call. For this example, we are sending the stream object in because we will need to call EndRead and close the stream.

    The method that we create to be called at the end of the call would look something like this:

    static void CompleteRead(IAsyncResult result)
    {
        Console.WriteLine("Read Completed");
    
        FileStream strm = (FileStream) result.AsyncState;
    
        // Finished, so we can call EndRead and it will return without blocking
        int numBytes = strm.EndRead(result);
    
        // Don't forget to close the stream
        strm.Close();
    
        Console.WriteLine("Read {0} Bytes", numBytes);
        Console.WriteLine(BitConverter.ToString(buffer));
    }
    

    Other techniques are Wait-until-done and Polling.

    Wait-Until-Done Model The wait-until-done model allows you to start the asynchronous call and perform other work. Once the other work is done, you can attempt to end the call and it will block until the asynchronous call is complete.

    // Make the asynchronous call
    strm.Read(buffer, 0, buffer.Length);
    IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null);
    
    // Do some work here while you wait
    
    // Calling EndRead will block until the Async work is complete
    int numBytes = strm.EndRead(result);
    

    Or you can use wait handles.

    result.AsyncWaitHandle.WaitOne();
    

    Polling Model The polling method is similar, with the exception that the code will poll the IAsyncResult to see whether it has completed.

    // Make the asynchronous call
    IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null);
    
    // Poll testing to see if complete
    while (!result.IsCompleted)
    {
        // Do more work here if the call isn't complete
        Thread.Sleep(100);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I use: connectionSocket.BeginSend(data, 0, length, SocketFlags.None, out error, new AsyncCallback(SendDataDone), state); Is SendDataDone
Essentially, what are the differences between these beyond the obvious? When should I use
I read the Use AsyncCallback , there is this code snippet: using System; using
In c# when I use the code : public System.IAsyncResult BeginConnect(System.Net.EndPoint remoteEP, System.AsyncCallback callback,
Use Case Show a photo uploaded by the user in a square box with
use C#,want to upload excel file on google doc. bellow syntax use to upload
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
use strict; use warnings; use Time::HiRes qw(sleep); use Test::WWW::Selenium; use Test::More no_plan; use Test::Exception;
use the [] symbol in the name of the form field you are submitting
Use of gradient images is very common among developers for styling a page. Gradient

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.