private void EnsureIsCurrentThread(int threadId) { uint actualCurrentId; var hr = _systemObjects.GetCurrentThreadSystemId(out actualCurrentId); if (hr != HResult.Ok) { return; } if (actualCurrentId == threadId) { return; } uint desiredEngineId; hr = _systemObjects.GetThreadIdBySystemId((uint)threadId, out desiredEngineId); if (hr != HResult.Ok) { throw new Exception($"Could not get desired thread ('{threadId}') - error code '{hr.ToString("X8")}'."); } hr = _systemObjects.SetCurrentThreadId(desiredEngineId); if (hr != HResult.Ok) { throw new Exception($"Could not set desired thread ('{threadId}') - error code '{hr.ToString("X8")}'."); } State.CurrentThread = threadId; }