Is it possible to create a Window Message Dispatch Thread?

Question: See subject.

Working code appreciated.  If not possible, explanation appreciated.

Restatement:
Why doesn't something like the following work?  What, if anything, would?

DWORD WINAPI MsgDispatchThread (LPVOID lpParam) {
  HWND hWnd = (HWND) lpParam;
  // While (Get) translate, dispatch
}

INT WINAPI WinMain(hInstance, ...) {
  HWND hWnd = MyCreateWindow(hInstance);
  HANDLE thread = CreateThread(NULL, 0, MsgDispatchThread, hWnd, 0, NULL);
  Sleep(3000); // We listen for 3s, then stop caring unless they've said something interesting.
  CloseHandle(thread);
}
LVL 1
ORBiTrusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DanRollinsCommented:
I would recommend against it.
The window to which the messages get dispatched will have been created by the other (main) thread and other program initialization my be thread-specific as well (e.g, intialize winsock, initializ COM handling, etc.)

Although you may not be using MFC, it makes a significant distinction between "worjker threads" and "U/I threads".  I tell you that only as support of my main point -- they would not have made that distinction if it were not critical.

Your safest route is to multi-threading is to isolate program tasks that do *no* user interface operations (accepting keyboard/mouse input, outputing to to a window, running a dialog, etc.) and create threads that do thos atomic tasks.  As soon as you have multiple threads interacting with a user, you will find yourself in a very complex situation -- when those complexities can usually be avoided.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ORBiTrusAuthor Commented:
Increasing points for a great answer given.
0
ORBiTrusAuthor Commented:
Of course!  That makes a great deal of sense.

Thanks a ton.  I've elevate the points to 500 - you deserve the full 2000!
(Not that you need them, but the reward should be according to the answer).
0
ORBiTrusAuthor Commented:
Thought I'd follow up; Windows certainly does make quite a distinction...

This didn't work:

DWORD WINAPI MainThread(LPVOID lpParam) {
  MSG Msg;
  HWND hWnd = *((HWND*) lpParam);

  while (GetMessage(&Msg, hWnd, 0, 0)) {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
  }
}

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdLine) {
  HWND hWnd = MyCreateWindow(hInstance);
  HANDLE thread = CreateThread(NULL, 0, MainThread, &hWnd, 0, NULL);
  Sleep(5000);
}


----- But as suggested, this does:

DWORD WINAPI MainThread(LPVOID lpParam) {
  MSG Msg;
  HWND hWnd = MyCreateWindow( *((HINSTANCE) hInstance) );

  while (GetMessage(&Msg, hWnd, 0, 0)) {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
  }
}

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdLine) {
  HANDLE thread = CreateThread(NULL, 0, MainThread, &hInstance, 0, NULL);
  Sleep(5000);
}

------

No messages were recieved in the former; not even WM_DESTROY.  But in the latter, all messages were recieved.

Intriguingly, it would appear that I can have the thread set an m_hWnd, and post messages to it from other threads.

Obviously, I still don't understand how Windows handles the scoping of variables and threads.  That's alright though, this has been all quite educational.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.