I try to get the initialisation of the space navigator device done in a thread. I want to explain why: The device should work with an ActiveX viewer on the web. The viewer starts up in about 1 second when the device is attached but it takes about 7 seconds when the device is detached. As I don't want the user to wait in that case, I have put the initialization code into a thread.
I already found out that I have to call CoInitalize(NULL) in the thread and the thread must live until the main thread ends. Otherwise I would get marshalling errors.
The problem: The program runs now without errors, but it does no longer react on the inputs from the device. Here is the code of the thread:
Code: Select all
void *Thread3DxDevice(void *data)
{
HRESULT hr;
CComPtr<IUnknown> _3DxDevice;
CoInitialize(NULL);
CCnsweb3dCtrl *ctrl = (CCnsweb3dCtrl *)data;
ctrl->DebugLog(" Thread 3Dx started.\n");
// Create the device object
hr = _3DxDevice.CoCreateInstance(__uuidof(TDxInput::Device));
if (SUCCEEDED(hr))
{
CComPtr<TDxInput> _3DxSimpleDevice;
hr = _3DxDevice.QueryInterface(&_3DxSimpleDevice);
if (SUCCEEDED(hr))
{
// Associate a configuration with this device
_3DxSimpleDevice->LoadPreferences(_T("CNSwebViewer"));
// Connect to the driver
_3DxSimpleDevice->Connect();
// Get the interfaces to the sensor and the keyboard;
ctrl->SetG3DSensor(_3DxSimpleDevice->Sensor);
ctrl->SetG3DKeyboard(_3DxSimpleDevice->Keyboard);
}
}
while (42)
Sleep(250);
ctrl->DebugLog(" Thread 3Dx ended.\n");
return 0;
}
Code: Select all
if (g3DSensor)
{
try {
CComPtr<TDxInput> pRotation = g3DSensor->Rotation;
CComPtr<TDxInput> pTranslation = g3DSensor->Translation;
// Check if the cap is still displaced
if (pRotation->Angle > 0. || pTranslation->Length > 0.)
{
...
I also tried to do the reading of the results in the thread that initializes, but that also does not work (also 0 in rotation and translation).
Thanks for your help.