很好的好好 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 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印