# 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 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.

# Building and Running the Sample

Note
If you are running Visual C++ Express Edition, you might need to install the Platform SDK before running this sample. For information on how to do this, see How to: Use Visual C++ Express Edition with the Microsoft Platform SDK.

#### 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.