1

我正在研究一些使用 MFC 的 UI 线程来实现管理器线程工作线程机制的遗留代码。以前在 MFC GUI 应用程序下运行的代码,但现在它在一个单独的 dll 中,并且可以从 GUI 应用程序和控制台应用程序运行。

管理线程、工作线程和主应用程序通过线程消息进行通信(工作线程实际上并不需要向管理线程发送消息,但这是它最初实现和工作的方式,所以你去)。

现在,当我从控制台应用程序运行代码时,会处理从主线程发送到管理器线程的消息并调用我的处理程序。只有当我尝试从管理线程向工作线程发送消息时,我才会遇到问题。调用PostThreadMessage成功,但从未调用处理程序。这种行为在一个普通的控制台应用程序和一个 Win32 控制台应用程序(包括一个带有所有 MFC 好东西的预编译头文件)中都重现了。

我找到了这篇旧的 Microsoft 文章: http: //support.microsoft.com/kb/142415但我不得不承认我并没有真正理解它。我尝试按照它的建议覆盖该PreTranslateMessage函数并在那里显式处理我的自定义消息,但调用后从未调用该函数PostThreadMessage

我试图在下面的示例中重现该问题,在我的示例中,甚至从未处理到管理器线程的消息,这证实了我对我做错了什么的怀疑。

编辑:我添加了我的示例代码中缺少的缺失InitInstanceExitInstance重载ManagerThread,正如 MarsRover 所建议的那样,ManagerThread现在确实发送了消息,但WorkerThread没有发送消息,这准确地重现了我在原始代码中遇到的问题。示例代码:

//Common.h

//update the progress message
#define WM_START_RUN (WM_USER + 1)

//update the progress message
#define WM_JOB_DONE (WM_USER + 2)

//run thread has finished
#define WM_RUN    (WM_USER + 3)

// ManagerThread.h
class ManagerThread : public CWinThread
{
    DECLARE_DYNCREATE(ManagerThread)
protected:
    ManagerThread(){}           // protected constructor used by dynamic creation
    virtual ~ManagerThread();
    BOOL InitInstance();
    int ExitInstance();
    std::vector<WorkerThread*> m_WorkerThreads;
    int numOfJobs;
    DECLARE_MESSAGE_MAP()
    afx_msg void OnStartRun(WPARAM wParam, LPARAM lParam);
    afx_msg void OnJobDone(WPARAM wParam, LPARAM lParam);
    afx_msg void OnQuit(WPARAM wParam, LPARAM lParam);
};

//WorkerThread.h
class WorkerThread : public CWinThread
{
    DECLARE_DYNCREATE(WorkerThread)

protected:
    WorkerThread(){}        // protected constructor used by dynamic creation
    virtual ~WorkerThread(){}
    virtual BOOL InitInstance();
    virtual int ExitInstance();

public:
    void SetManager(CWinThread* pManager) {m_Manager = pManager;}
    void SetID(int _id) {id = _id;}
protected:
    int id;
    CWinThread* m_Manager;
    DECLARE_MESSAGE_MAP()
    afx_msg void OnRun(WPARAM wParam, LPARAM lParam);
    afx_msg void OnQuit(WPARAM wParam, LPARAM lParam);
};

// ManagerThread.cpp

IMPLEMENT_DYNCREATE(ManagerThread, CWinThread)

ManagerThread::~ManagerThread() {
    while(!m_WorkerThreads.empty()) {
        std::vector<WorkerThread*>::iterator it = m_WorkerThreads.begin();
        (*it)->PostThreadMessage(WM_QUIT, 0, 0);
        m_WorkerThreads.erase(it);
    }
}

BOOL CFilterManagerThread::InitInstance()
{
    return CWinThread::InitInstance();
}


int CFilterManagerThread::ExitInstance()
{
    return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(ManagerThread, CWinThread)
    ON_THREAD_MESSAGE(WM_START_RUN, OnStartRun)
    ON_THREAD_MESSAGE(WM_JOB_DONE, OnJobDone)
    ON_THREAD_MESSAGE(WM_QUIT, OnQuit)
END_MESSAGE_MAP()

void ManagerThread::OnJobDone( WPARAM wParam, LPARAM lParam) {
    numOfJobs--;
    if (!numOfJobs) {
        OnQuit(0,0);
    }
}

void ManagerThread::OnStartRun(WPARAM wParam, LPARAM lParam) {
    numOfJobs = (int) wParam;
    for (int i = 0; i < numOfJobs; i++) {
        WorkerThread *newThread = (WorkerThread*)AfxBeginThread(RUNTIME_CLASS(WorkerThread), THREAD_PRIORITY_LOWEST, 0, CREATE_SUSPENDED);
        newThread->SetID(i);
        newThread->SetManager(this);
        m_WorkerThreads.push_back(newThread);
        newThread->ResumeThread();
        Sleep(1000); //sleep 1 second before sending message to allow the thread to strat running
        newThread->PostThreadMessage(WM_RUN, 0, 0);
    }
}

void ManagerThread::OnQuit(WPARAM wParam, LPARAM lParam) {
    AfxEndThread(0);
}

// WorkerThread.cpp

IMPLEMENT_DYNCREATE(WorkerThread, CWinThread)

BOOL WorkerThread::InitInstance() {
    // TODO:  perform and per-thread initialization here
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    return TRUE;
}

int WorkerThread::ExitInstance() {
    // TODO:  perform any per-thread cleanup here

    //uninitialize the COM library
    CoUninitialize();
    return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(WorkerThread, CWinThread)
    ON_THREAD_MESSAGE(WM_RUN, OnRun)
    ON_THREAD_MESSAGE(WM_QUIT, OnQuit)
END_MESSAGE_MAP()

void WorkerThread::OnRun(WPARAM wParam, LPARAM lParam) {
    cout << id <<endl;
    m_Manager->PostThreadMessage(WM_JOB_DONE, id, 0);
}
void WorkerThread::OnQuit(WPARAM wParam, LPARAM lParam) {
    AfxEndThread(0);
}

并在main

        ManagerThread *manager = (ManagerThread*)AfxBeginThread(RUNTIME_CLASS(ManagerThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
        manager->ResumeThread();

        Sleep(1000); //sleep 1 second before sending message to allow the thread to start running

        manager->PostThreadMessage(WM_START_RUN, 10, 0);
        while(true){}

这是一个粗略的样本。当然,在我的原始代码中,我使用了更好的机制来确保同步Sleepwhile(true)避免程序在管理器线程结束之前结束。但它重现了我遇到的问题,所以我没有看到增加任何复杂性的意义。

4

1 回答 1

2

弄清楚问题出在哪里。CoInitializeEx问题是对in的调用WorkerThread::initInstance。显然,该调用长时间阻塞了线程的初始化,甚至超过了示例代码中的 Sleep(1000)。所以我在创建消息队列之前发布了消息。因此,请按照MSDN的说明进行操作:

消息发布到的线程必须已经创建了消息队列,否则对 PostThreadMessage 的调用将失败。使用以下方法来处理这种情况。

创建一个事件对象,然后创建线程。

在调用 PostThreadMessage 之前,使用 WaitForSingleObject 函数等待事件设置为信号状态。

在将要发布消息的线程中,调用此处所示的 PeekMessage 以强制系统创建消息队列。

PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)

设置事件,以指示线程已准备好接收发布的消息。

上一个问题CEvent中,我为每个线程类创建了一个成员并将其更改InitInstance为:

BOOL CFilterWorkerThread::InitInstance()
{
    BOOL worked=CWinThread::InitInstance();
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    MSG msg;
    PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
    m_ControllerThreadReady.SetEvent();
    return TRUE;
}

为了在我将偶数设置为true之前强制初始化消息队列。然后在ManagerThreadWaitForSingleObject发布任何消息之前打电话给WorkerThread.

于 2012-10-08T13:31:18.990 回答