AutoThread Attributes Sample: Demonstrates the Use of CAtlAutoThreadModule
The AutoThread sample demonstrates using CAtlAutoThreadModule. The server is implemented in the Server.exe file. The module of the EXE is derived from CAtlAutoThreadModuleinstead of CAtlModule.
The AutoThread sample is the nonattributed version of this sample.
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.
To build and run this sample
Open the solution file AutoThread.sln.
From the Build menu, click Build Solution. This will build and register the client and the server.
Start two instances of the ActiveX Control Test Container. See Testing Properties and Events with Test Container for information on how to access the test container.
Insert one of the client controls (CAutoCtl Class) into each of the test containers.
Click one of the controls and notice that it takes one second for the server to return.
Position and resize the test containers so that both of them are visible at the same time.
Click one of the controls and then quickly click the other control. Notice that they finish waiting at approximately the same time. (If CComAutoThreadModule were not used, the first control would finish after one second, but the second control would not finish until a full second after the first control finished. The second call to sleep would not occur until the first had finished.) You can use the Delay(PropGet) and Delay(PropPut) methods to adjust the number of milliseconds the server sleeps for. If set properly, the second call to sleep may return before the first call to sleep.
How the Sample Works
The server interface has a single method: Sleep. This method puts the server thread to sleep for a given amount of time. The client portion of the sample is an ActiveX control that invokes the server's sleep method when the user clicks the control. The client also has a property named Delay that represents how long the server thread will sleep. The control displays the text "Ready" when it is waiting for a user click. The text "Waiting" is displayed when the control is waiting for the server to finish sleeping.
This sample uses the following attributes:
AutoThread/AutoClient coclass, dual, emitidl, helpstring, id, in, module, object, out, pointer_default, progid, propget, propput, registration_script, retval, threading, uuid, version, vi_progid
AutoThread/AutoServer coclass, dual, emitidl, helpstring, id, module, object, pointer_default, progid, threading, uuid, version, vi_progid
This sample uses the following keywords:
AtlGetObjectSourceInterface; BEGIN_SINK_MAP; CoCreateInstance; DECLARE_CLASSFACTORY_SINGLETON; IDispEventSimpleImpl::DispEventAdvise; END_SINK_MAP; IConnectionPointContainerImpl; IConnectionPointImpl; IDispEventImpl; OLE2CT; SINK_ENTRY_EX; SysAllocString; USES_CONVERSION; VARIANT; VariantClear; VariantCopy
Some of the samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.