Click here to Skip to main content
6,293,171 members and growing! (12,201 online)
Email Password   helpLost your password?
Languages » C# » Utilities License: The Code Project Open License (CPOL)

Generating synchronous method stubs from asynchronous method pairs

By Adam Langley

Writing asynchronous code requires repetative programming if you have synchronous method stubs aswell.This code generator creates them for you.
Posted:8 Feb 2008
Updated:8 Feb 2008
Views:4,513
Bookmarked:2 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
0 votes for this article

Introduction

During the development of a GSM Modem API, I found I was repeating alot of code for encapsulating the synchronous versions of asynchronous method templates.
The common pattern for such code, implements asynchronous methods with the same name as the synchronous versions, by prefixing the begin and end method with 'Begin', and 'End' respectively.
This means, that for each set of 2 asynchronous method pairs, you need another method to synchronize them - in a library of any significant size, this makes for considerably more work.

This article explains how to write a custom tool which utilises the content of the asynchronous class, to generate all the synchronous stubs, for methods which match the naming convention.

Because I have found this custom tool so helpful, I have folded it into the Missing Bits product. You can search for this product on CodeProject to obtain a copy containing a version of this custom tool along with many other useful additions to Visual Studio.

Background and Prerequisites

This article is not a tutorial on asynchronous programming, merely an overview on utilising this particular custom tool. A basic understanding of Custom Tools would be greatly helpful, however, everything is provided in the zip file for Visual Studio 2005 registration.

Basic Architecture

This custom tool is designed to be applied to a code file containing a single class, wherein are one or more method signatures mathing the required asynchronous code pattern.
The custom tool then parses these methods, determining from their parameters and return types, what the synchronous method should look like.

Method Pattern

Both the beginner, and completion, async methods must conform to the following patterns so that the custom tool may identify, and interpret them.

The following example uses a basic read operation to demonstrate method signatures that will be recognised by the custom tool.

Example Begin Method

public IAsyncResult BeginRead(byte[] buffer, int length, int offset, AsyncCallback callback, object state)

The method must return IAsyncResult, and the last 2 arguments must be AsyncCallback, and an object for state.

Example End Method

public int EndRead(IAsyncResult result)

The method returns the same type as the synchronous version, and takes just the IAsyncResult as the sole argument.

Code Generated by the Custom Tool follows:

public virtual int Read( byte[] buffer, int length, int offset) {
    System.IAsyncResult result = base.BeginRead(buffer, length, offset, null, null);
    return base.EndRead(result);
} 

Installation

The SynchronizerCustomTool assembly must be installed in the GAC, then run the reg file to install the required registry keys. Then, to use the custom tool, set the 'Custom Tool' property of your C# code file to "SynchronizerCustomTool" - this will result in the generation of another C# code file containing the synchronized methods.

Credits

This code uses a C# parser library written by Debreuil Digital Works and actively maintained by Denis Erchof, and distributed under the BSD license.

History

  • 2008.02.09 Initial Publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Adam Langley


Member
"Find a job you love, and you'll never work a day in your life."

Adam Langley is a software engineer in Auckland, New Zealand.

Software development is his personal passion, he takes pride in his work, and likes to share his experiences with the development community.

When he's not coercing computers, you'll find him riding his motorcycle, or attempting to carve something creative from bone.
Occupation: Software Developer (Senior)
Location: New Zealand New Zealand

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralHi Adam PinmemberDaniel M. Camenzind2:26 13 Feb '08  
QuestionThis is a cool idea PinmemberMaxGuernsey13:56 11 Feb '08  
AnswerRe: This is a cool idea PinmemberAdam Langley14:17 11 Feb '08  
GeneralRe: This is a cool idea PinmemberMaxGuernsey19:44 11 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Feb 2008
Editor:
Copyright 2008 by Adam Langley
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project