2012-03-30 12 views
0

私はWorkerthreadからメインウィンドウ(ダイアログ)へデータを渡す方法についていくつかのスレッドを読んでいますが、まだ理解しておらず、まだ助けが必要です。 私のworkerthreadはいくつかの計算を処理し、各ループ中に結果をGUIの編集に表示する必要があります。私が知っているMFC - PostMessageを使用したPostMessageによるデータのGUIへの投稿

//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); 

次probemは...、私はのPostMessageを使用する必要がありますが、私がやっている計算は、制御要素を意味するので、私はこの問題を解決する方法がわからない、その時に私のスライダコントロールを新しい値を取得すると、スレッドプロシージャはそれを認識し、スライダの値を更新する必要があります。どうすればそれをすることができますか?

+0

スライダの位置をパラメータとしてスレッドに渡すことはできますか? – Jeeva

答えて

0

ワーカースレッドからスライダーの位置を読み取る代わりに、UIスレッドでスライダーの位置を読み取り、ワーカースレッドが使用できるようにします。

私は実際にこの分野の専門家ではありませんが、hereのようにいくつかのワーカースレッドを実行しました。

あなたのケースでは、スライダの現在の位置を保持するために、上にリンクされた記事で使用されている「実行中」フラグのような静的変数を作成します。次に、OnHScrollハンドラで適切な値に設定します。

1つのスレッドからその変数に書き込むだけであれば、同期の問題は発生しません。ワーカースレッドから

0

:UIスレッドから

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

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を参照してください。

関連する問題