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

The Archive Base Latest Questions

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

I am implementing an asynchronous command pattern for the client class in a client/server

  • 0

I am implementing an asynchronous command pattern for the ‘client’ class in a client/server application. I have done some socket coding in the past and I like the new Async pattern that they used in the Socket / SocketAsyncEventArgs classes.

My async method looks like this: public bool ExecuteAsync(Command cmd); It returns true if the execution is pending and false if it completed synchronously. My question is: Should I always call the callback (cmd.OnCompleted), even in the event of an exception? Or should I throw exceptions right from ExecuteAsync?

Here are some more details if you need them. This is similar to using SocketAsyncEventArgs, but instead of SocketAsyncEventArgs my class is called SomeCmd.

SomeCmd cmd = new SomeCmd(23, 14, 10, 'hike!'); cmd.OnCompleted += this.SomeCmd_OnCompleted; this.ConnectionToServer.ExecuteAsync(cmd); 

As with the Socket class, if you need to coordinate with your callback method (SomeCmd_OnCompleted in this case), you can use the return value of ExecuteAsync to know if the operation is pending (true) or if the operation completed synchronously.

SomeCmd cmd = new SomeCmd(23, 14, 10, 'hike!'); cmd.OnCompleted += this.SomeCmd_OnCompleted; if( this.ConnectionToServer.ExecuteAsync(cmd) ) {     Monitor.Wait( this.WillBePulsedBy_SomeCmd_OnCompleted ); } 

Here is a greatly simplified version of my base classes, but you can see how it works:

class Connection {     public bool ExecuteAsync(Command cmd)     {         /// CONSIDER: If you don't catch every exception here         /// then every caller of this method must have 2 sets of                 /// exception handling:         /// One in the handler of Command.OnCompleted and one where ExecuteAsync         /// is called.         try         {         /// Some possible exceptions here:         /// 1) remote is disposed. happens when the other side disconnects (WCF).         /// 2) I do something wrong in TrackCommand (a bug that I want to fix!)             this.TrackCommand(cmd);             remote.ServerExecuteAsync( cmd.GetRequest() );             return true;         }         catch(Exception ex)         {             /// Command completing synchronously.             cmd.Completed(ex, true);             return false;         }     }     /// <summary>This is what gets called by some magic when the server returns a response.</summary>     internal CommandExecuteReturn(CommandResponse response)     {         Command cmd = this.GetTrackedCommand(response.RequestId);         /// Command completing asynchronously.         cmd.Completed(response, false);     }      private IServer remote; }  abstract class Command: EventArgs {     internal void Completed(Exception ex, bool synchronously)     {         this.Exception = ex;          this.CompletedSynchronously = synchronously;          if( this.OnCompleted != null )         {             this.OnCompleted(this);         }     }      internal void Completed(CommandResponse response, bool synchronously)     {         this.Response = response;         this.Completed(response.ExceptionFromServer, synchronously)     }      public bool CompletedSynchronously{ get; private set; }      public event EventHandler<Command> OnCompleted;      public Exception Exception{ get; private set; }      internal protected abstract CommandRequest GetRequest(); } 
  • 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-10T21:52:53+00:00Added an answer on May 10, 2026 at 9:52 pm

    throwing an exception from the dispatch point may or may not be useful

    calling the callback passing an exception argument requires the completion callback to do 2 distinct things

    a second callback for exception reporting might make sense instead

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • 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 The core issue here is that direct field access is… May 11, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer $facebook->api_client->profile_setFBML is absolutely correct afair. Go to the App Page… May 11, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer While there are several choices of plugins for NetBeans or… May 11, 2026 at 11:46 pm

Related Questions

I am developing a page view counter to track the amount of views a
I can't seem to figure out why I am getting an InvalidCastException running the
I am implementing an HTML form with some checkbox input elements, and I want
I am implementing an undo/redo buffer with generic LinkedList. In this state: [Top] state4

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.