Udostępnij za pośrednictwem


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 provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Building and Running the Sample

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.

How the Sample Works

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.

See Also

Reference

Compiler COM Support

Other Resources

Compiler COM Support Samples

General Samples