|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionDuring the development of a GSM Modem API, I found I was repeating alot of code for encapsulating the synchronous versions of asynchronous method templates. 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 PrerequisitesThis 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 ArchitectureThis 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. Method PatternBoth 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 Methodpublic 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 Methodpublic 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); } InstallationThe 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. CreditsThis code uses a C# parser library written by Debreuil Digital Works and actively maintained by Denis Erchof, and distributed under the BSD license. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||