FREETHRD Sample: Multithreaded Client and Free-Threaded Server with Compiler COM Support

This sample demonstrates a multithreaded client and free-threaded server with compiler COM support.

This sample consists of the following parts:

  • Freclien, a multithreaded client

  • Freserve, a free-threaded in-process server

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

To build and run this sample

  1. Open the solution freethrd.sln.

  2. Build the server project and then build the client project.

  3. Make the client project the startup project (right-click the project node and click Set as StartUp Project) and run the sample.

The Freclien sample spawns multiple threads to create and use the COBall COM object provided by the Freserve free-threaded server. The COBall object itself spawns no threads; instead, it passively responds to IBall interface requests from many possible client threads. The Freclien client creates and controls one COBall object through the IBall interface that the object exposes. As three of Freclien's threads move the ball through calls to IBall::Move, the remaining main thread uses a system timer to obtain timed updates of the COBall object's position, size, and color. This main thread uses that data, obtained by calling IBall::GetBall, to display graphical snapshot images of the ball in the client's main window.

In the Freserve sample, the COBall object internally updates its color property to reflect the last thread that called the object's Move method. The display thread uses this data for each ball image it displays. As the ball moves, it changes color to reflect each thread that moves the ball. As the ball moves, it also leaves a trail that provides a striking visual history of these passing threads. This trail demonstrates that, with COM's free-threading model, every thread that makes interface requests to the same object accesses the object on the calling thread. Each different color of the single ball object represents a different calling thread.

This sample uses native compiler COM support. It gives an example of a custom COM interface that marshals the RECT and POINT structures with the free-threaded model. It demonstrates the use of the exclude attribute of the #import directive.

Community Additions