MFC和AfxBeginThread的线程调度问题

发布于 2024-11-30 20:06:41 字数 572 浏览 0 评论 0原文

我正在使用 AfxBeginThread 在 MFC 中创建一个工作线程,但该线程未得到调度。这是代码:

CWinThread* worker = AfxBeginThread(initialUpdateWorkerThread, this);
DWORD dwExitCode = 0;
while(GetExitCodeThread(worker->m_hThread, &dwExitCode))
{
    if(dwExitCode != STILL_ACTIVE)
        break;
    ::Sleep(100);
}

当我运行这个循环时,这个循环只是活锁,因为initialUpdateWorkerThread永远不会被调用(我在它的顶部放置了断点和消息框),所以dwExitCode始终是STILL_ACITVE。但是,如果我在循环之前但在 AfxBeginThread 之后调用 AfxMessageBox,则调用该函数。这让我觉得不知何故我没有调用正确的函数来安排线程,但是对 AfxMessageBox 的调用导致它被安排。

如何强制调度线程?我认为睡眠可以做到这一点,但在这种情况下似乎没有。

I'm creating a worker thread in MFC with AfxBeginThread, but the thread is not getting scheduled. Here's the code:

CWinThread* worker = AfxBeginThread(initialUpdateWorkerThread, this);
DWORD dwExitCode = 0;
while(GetExitCodeThread(worker->m_hThread, &dwExitCode))
{
    if(dwExitCode != STILL_ACTIVE)
        break;
    ::Sleep(100);
}

When I run this, this loop just livelocks because initialUpdateWorkerThread is never called (I've put break points and message boxes at the top of it) so dwExitCode is always STILL_ACITVE. But if I put in a call to AfxMessageBox before the loop but after AfxBeginThread then the function is called. This makes me think that somehow I'm not calling the right function to get the thread scheduled, but a call to AfxMessageBox causes it to get scheduled.

How can I force the thread to be scheduled? I would think sleep would do that, but in this case it doesn't seem to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2024-12-07 20:06:41

您的工作线程可能正在尝试向主线程发送消息,但由于您没有在主线程上处理消息,因此工作线程只是等待。您可以通过简单地进入调试器来查看工作线程正在做什么来确认这一点。

Your worker thread is probably trying to send your main thread a message, but since you aren't processing messages on on the main thread, the worker thread simply waits. You can confirm this by simply breaking into the debugger to see what the worker thread is doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文