【问题标题】:MFC - Posting data via PostMessage to the GUIMFC - 通过 PostMessage 将数据发布到 GUI
【发布时间】:2012-03-30 10:05:03
【问题描述】:

我阅读了一些关于如何将数据从 Workerthread 传递到主窗口(对话框)的线程,但我仍然不明白,仍然需要帮助。 我的工作线程应该处理一些计算并在每个循环期间显示结果以在 GUI 上进行编辑。我知道,我应该使用 PostMessage 但由于我正在做的计算意味着一个控制元素,我不知道如何解决这个问题......

 //CWorkerThreadMgr.h manages the second thread
HRESULT Start (HWND hWnd);// from where the workerthread will be started 

 HWND  m_hWnd ;    //Window handle to the UI ;

 HANDLE m_hTread;  //handle of the worker thread

 static UINT WINAPI ThreadProc( LPVOID lptest ); 

static UINT WINAPI ThreadProc( LPVOID lptest ) 

{    
  CWorkerThreadMgr* pCalculateMgr = reinterpret_cast< CWorkerThreadMgr*(lptest);

//The following operation:rand() *m_Slider.GetPos() should

//should be calculated and the result displayed each time in the edit box in the gui

for( UINT uCount = 0; uCount < 40; uCount++ ){ 

pCalculateMgr->rand() *m_Slider.GetPos();//?don't allowed to touch the gui!! 

PostMessage(pCalculateMgr-> m_hWnd, WM_SENDCALCULATED_VALUE,wparam(rand() *m_Slider.GetPos(),0); 
} 
} 

LRESULT CStartCalculationDlg::OnSendCalculatedValue( WPARAM Result, LPARAM )

{ 
    // The resut should be displayed in the edit box

      m_Calculation.Format(_T("%d"),???); 
      SetDlgItemText(IDC_CALCULATION, m_Calculation); 

     return 1; 
} 

void CStartCalculationDlg::OnHScroll(UINT nSBCode, UINT nPos,CScrollBar* pScrollBar) 
{ 
  m_SliderValue.Format(_T("%d"),m_Slider.GetPos()); 
  SetDlgItemText(IDC_SLIDER_VALUE,m_SliderValue); 
} 

// Implementation in the CStartCalculationDlg.h 

CWorkerThreadMgr   m_WorkerThreadMgr  //instance of the WorkerThreadMgr 
CSliderCtrl  m_Slider  //member variable of the slider control 
CString m_SliderValue  // member variable of the edit box, where the current value of the 
                       //slider will be displayed 
CString  m_Calculation // member variable of the edit box where the calculated 
                       //result from  the workerthread will be displayed via PostMessage 
afx_msg LRESULT OnSendCalculatedValue( WPARAM, LPARAM ); 
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); 

下一个问题是,当我的滑块控件被移动并获得一个新值时,线程过程应该知道它并更新滑块的值。我怎样才能做到呢?

【问题讨论】:

  • 能否将滑块位置作为参数传递给线程?

标签: multithreading mfc postmessage


【解决方案1】:

不是从工作线程中读取滑块位置,而是在 UI 线程中读取它并使其可供工作线程使用。

我真的不是这个领域的专家,但我已经按照here 的说明做了一些工作线程。

在您的情况下,我要做的是创建一个静态变量,例如上面链接的文章中使用的“运行”标志,以保持滑块的当前位置。然后,在 OnHScroll 处理程序中将其设置为适当的值。

只要您只从一个线程写入该变量,就不会出现同步问题。

【讨论】:

    【解决方案2】:

    来自工作线程:

    m_data = foo();
    PostMessage(hWndMain, UWM_SOME_CUSTOM_MESSAGE, 0, 0);
    

    来自 UI 线程:

    LRESULT CMainFrame::OnSomeCustomMessage(WPARAM wParam, LPARAM lParam)
    {
        CMyData data = m_pWorker->GetData();
        // Do stuff...
        return 0;
    }
    

    GetData 必须由临界区保护:

    CMyData CMyWorker::GetData()
    {
         // This critical section is used in the worker thread too, whenever m_data is accessed.
         m_lock.Lock();
         CMyData data = m_data;
         m_lock.Unlock();
    
         return data;
    }
    

    请参阅 MSDN 上的 CCriticalSection

    【讨论】:

      猜你喜欢
      • 2011-04-18
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 2016-06-04
      • 1970-01-01
      • 2023-01-01
      相关资源
      最近更新 更多