Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3237930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:47:44+00:00 2026-05-17T17:47:44+00:00

I have a thread that creates COM objects that require a single threaded apartment.

  • 0

I have a thread that creates COM objects that require a single threaded apartment.

Originally, this thread’s main function put it into a WaitForMultipleObjects loop. Evidently this is a problem, because it prevents the COM message pump from doing it’s job.

I replaced this with MsgWaitForMultipleObjects as a solution, but now I’m running into an issue: MsgWaitForMultipleObjects sometimes (often) returns WAIT_FAILED, but doesn’t set an error.

The code handles a WAIT_FAILED return value by just continuing and trying to call MsgWaitForMultipleObjects again. The call to MsgWaitForMultipleObjects may return WAIT_FAILED a few times (the most I’ve seen is 9), but then it suddenly works with no problem.

The code is written so that this could potentially go into an infinite loop if the function were returning WAIT_FAILED for a valid reason. I know I should fix this, but at the moment I’m considering it a ‘work around’ because the MsgWaitForMultipleObjects call will eventually succeed.

This code is being tested on Windows 7, Vista, and XP (all 32-bit, Windows 7 32- and 64-bit).

Does anyone have any idea why this is happening?

The relevant code:

bool run = true;
while (run)
{
    DWORD ret = MsgWaitForMultipleObjects(2, events, FALSE, INFINITE, 
        QS_ALLINPUT);

    switch (ret)
    {
        case WAIT_OBJECT_0:
        {
            ResetEvent(events[0]);
            ProcessWorkQueue();
            break;
        }

        case WAIT_OBJECT_0 + 1:
        {
            run = false;
            break;
        }

        case WAIT_OBJECT_0 + 2:
        {
            MSG msg;
            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                DispatchMessage(&msg);
            break;
        }

        case WAIT_FAILED:
        {
            Logger::Output(L"Wait failed in Notify::Run(), error is " 
                + boost::lexical_cast<std::wstring>(GetLastError()));
        }
    }
}

Example output would be:

Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
Wait failed in Notify::Run(), error is 0
// At this point, the wait succeeds

I believe the WAIT_FAILED return value is only occurring after the wait has been broken by a message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:47:45+00:00Added an answer on May 17, 2026 at 5:47 pm

    That shouldn’t be happening, and I sure can’t explain exactly why it does. I do have a few pointers, however.

    First, you’re not calling TranslateMessage() before DispatchMessage() in your message pump. That’s bad juju, and you don’t want bad juju anywhere near MsgWaitForMultipleObjects().

    You also might want to try explicitly calling MsgWaitForMultipleObjectsEx(), just in case it doesn’t exhibit the same problem:

    DWORD ret = MsgWaitForMultipleObjectsEx(2, events, INFINITE, QS_ALLINPUT, 0);
    

    Finally, it might be far-fetched but consider what’s happening after MsgWaitForMultipleObjects() returns and before GetLastError() is called. Disregarding the assignment to ret, I see an implicit call to std::wstring‘s constructor.

    Can you guarantee std::wstring‘s constructor doesn’t have some side-effect that clears the thread’s last error code? I sure can’t, so I’d move the call to GetLastError() into an good, old-fashioned, atomic assignment to a DWORD variable in the first line of the case statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread that, when its function exits its loop (the exit is
I have a thread that needs to be executed every 10 seconds. This thread
I have a C# application which uses a COM component. This COM component require
I have a VBA application that creates an instance of a COM object and
I have one thread that writes results into a Queue. In another thread (GUI),
In my Silverlight project I have a thread that fires every x milliseconds. In
In my C# program, I have a thread that represents a running test, which
I have a worker thread that is listening to a TCP socket for incoming
I've seen a number of examples that have a thread procedure that looks like
We have a Windows Service written in C#. The service spawns a thread that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.