程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> mfc-VS2013中MFC多線程編程的問題

mfc-VS2013中MFC多線程編程的問題

編輯:編程綜合問答
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個窗口發送字符串,但是只能對前面的窗口發送,後面的窗口發送不了,點一下後面的窗口,就對後面的窗口發送,前面的又不能發送了

最佳回答:


如果是STA模型,那麼你應該把數據都發給一個主線程,然後主線程再跟兩個窗口交互數據

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved