2

有没有办法从 threadId 中找出线程是否有消息队列?

基本上有一些windows api只有在线程有消息queue.window时才起作用

4

4 回答 4

7

You can use PostThreadMessage with the ID of the thread. If PostThreadMessage returns false and GetLastError returns ERROR_INVALID_THREAD_ID, the thread ID is invalid or the thread does not have a message queue.

More information http://msdn.microsoft.com/en-us/library/ms644946(VS.85).aspx

于 2009-11-24T12:07:35.363 回答
2

在多线程世界中,您无法正确考虑在查看时可能会发生变化的事情。线程消息队列特别有趣;它们是在线程需要它们时创建的。所以你能得到的唯一答案是“不是我上次检查的时候”。

因此,如果没有线程消息队列,大多数 Windows API 实际上不会失败。他们只是创造它。有一个例外,PostThreadMessage. 这将需要为另一个线程创建一个线程消息队列,可能在另一个线程创建自己的线程的同时。因此,它是唯一可能失败的 API。

PostThreadMessage失败就很糟糕吗?这告诉您(通过`GetLastError()),当Windows 尝试传递您的消息时,队列在重要的那一刻并不存在。

于 2009-11-26T14:56:27.643 回答
2

我认为IsGUIThread是需要的功能

于 2010-07-22T14:56:08.267 回答
0

您可以尝试从具有消息队列的线程中使用 AttachThreadInput。如果调用中引用的任一线程没有队列,我相信 AttachThreadInput 会失败。请注意,尽管 AttachThreadInput 可能由于其他原因而失败。

于 2009-11-24T11:29:06.523 回答