很好的好好
2015-05-13 02:49
采纳率: 100%
浏览 3.5k
已采纳

VS2013中MFC多线程编程的问题

我自定义一个类,继承自CWinThread,在类中实现对大漠插件的调用,在线程函数中向指定窗口发送字符串。

1、类的头文件

#pragma once
#ifndef UserThread_H
#define UserThread_H


// CUserThread

#include "Cdmsoft.h"

class CUserThread : public CWinThread
{

public:
    CUserThread();
    CUserThread(AFX_THREADPROC pfnThreadProc);
    CUserThread(AFX_THREADPROC pfnThreadProc, LONG task, LONG hwnd);//线程的启动函数 及实际任务索引
    static UINT ThreadFunc(LPVOID param);               //线程函数
    virtual ~CUserThread();

public:
    LONG m_nStartCounter;                               //成员变量,统计启用的线程数量
    LONG m_DestinationhWnd;                             
    LONG m_Task;                                        
    LONG m_X;                                           
    LONG m_Y;                                           
    Cdmsoft m_DM;                                       
    CString m_CurrentMission;                           
    BOOLEAN m_bEnable;                                  

private:
    virtual VOID Go();                                  // The "real" startup function

public:
    void MainMission();                                 
};


#endif


2、类的cpp

 // UserThread.cpp : 实现文件
//
#pragma once
#include "stdafx.h"
#include "UserThread.h"
#include "GlobalDefine.h"



// CUserThread
CUserThread::CUserThread()
{

}

CUserThread::CUserThread(AFX_THREADPROC pfnThreadProc) :CWinThread(pfnThreadProc, NULL)
{
    m_bAutoDelete = FALSE;

    // Set the pointer to the class to be the startup value.
    // m_pThreadParams is undocumented,
    // but there is no work-around.
    m_pThreadParams = this;
}

CUserThread::CUserThread(AFX_THREADPROC pfnThreadProc, LONG task,LONG hwnd) :CWinThread(pfnThreadProc, NULL)
{
    m_DestinationhWnd = hwnd;
    m_bAutoDelete = FALSE;
    m_pThreadParams = this;
    m_Task = task;

    ::CoInitialize(NULL);                                       //初始化线程COM库
    m_DM.CreateDispatch(_T("dm.dmsoft"), NULL);
}

CUserThread::~CUserThread()
{
    ::CoUninitialize();
}

// static
UINT CUserThread::ThreadFunc(LPVOID n)
{
    CUserThread* pThread = (CUserThread*)n;
    pThread->Go();
    return 0;
}

void CUserThread::Go()
{
    int n = m_nStartCounter; 


    switch (m_Task)
    {
    case 0:
        MainMission();
        break;
    case 1:
        break;
    case 2:
        break;
    default:
        break;  
    }
}

void CUserThread::MainMission()                             
{
    m_CurrentMission = _T("开始");
    while (m_bEnable)                   
    {

        if (!(m_DM.IsBind(m_DestinationhWnd)))
        {
            m_DM.BindWindow(m_DestinationhWnd, Display, Mouse, Keyboard, BindMode);


        }

        m_DM.SendString(m_DestinationhWnd, _T("m_DestinationhWnd"));
        m_DM.delay(1000);
    }
    m_CurrentMission = _T("停止");



    //::CoUninitialize();                                           //卸载COM库
}

现在创建了2个对象,分别对2个窗口发送字符串,但是只能对前面的窗口发送,后面的窗口发送不了,点一下后面的窗口,就对后面的窗口发送,前面的又不能发送了

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • oyljerry 2015-05-13 07:34
    已采纳

    如果是STA模型,那么你应该把数据都发给一个主线程,然后主线程再跟两个窗口交互数据

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • threenewbee 2015-05-13 02:52

    你的com组件的线程模型是什么?

    评论
    解决 无用
    打赏 举报
  • 很好的好好 2015-05-13 02:59

    大漠插件是仅支持com的STA线程模型

    评论
    解决 无用
    打赏 举报
  • Eleven 2015-05-14 08:24

    你不能再创建线程的时候讲窗口的HWND句柄作为线程函数的参数传递过去吗?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题