Freclien: Multithreaded Client of a Free-Threaded Server

Note   To use this sample, you must first build the Freserve 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 the IBall::GetBall method, to display graphical snapshot images of the ball in the client's main window.

As explained 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 free-threading model is in contrast to the apartment model presented in the Aptserve and Aptclien samples. In apartment model threading, each COM object is associated with an apartment thread. Calls from multiple client threads are switched by COM to the thread of the apartment that owns the object. Only one thread, the object's apartment thread, is permitted to execute the methods of the object. This provides a serialization of access to the object's data.

See the parent topic, COM Tutorial Samples, to download global tutorial files or any HTML files about this sample.

Building SDK Samples

This sample uses the following keywords:

addref; ballthreadproc; beginpaint; cguiball; closehandle; cmainwindow; cocreateinstance; cofreeunusedlibraries; coinitializeex; couninitialize; create; createellipticrgn; createsolidbrush; createthread; dcomok; defined; defwindowproc; delete_pointer; deleteobject; dispatchmessage; domenu; endpaint; errorbox; failed; fillrect; fillrgn; getball; getclassbrush; getclientrect; getdc; gethwnd; getlasterror; getmenu; getmessage; getstockobject; getsystemmetrics; gettickcount; getusername; hrmsg; initapplication; initinstance; killtimer; loadaccelerators; loadcursor; loadicon; loadimage; loadstringa; makefamilypath; makeintresource; messageboxa; move; paintball; paintwin; postmessage; postquitmessage; readhelp; readsource; readtutorial; registerclassex; release; release_interface; releasedc; reset; restart; rgb; settimer; showdialog; showwindow; style; succeeded; text; translateaccelerator; translatemessage; unicodeok; updatewindow; waitformultipleobjects; windowproc; winmain