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

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模型,那么你应该把数据都发给一个主线程,然后主线程再跟两个窗口交互数据

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 在rhel8中安装qemu-kvm时遇到“cannot initialize crypto:unable to initialize gcrypt“报错”
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 paddle库安装时报错提示需要安装common、dual等库,安装了上面的库以后还是显示报错未安装,要怎么办呀?
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀