Discussion:
Critical section-Updating the Dialog from Workerthread
(too old to reply)
s***@mvps.org
2012-03-30 01:46:31 UTC
Permalink
In your original post you said you wanted to compute something in the thread 40 times and display the result in the GUI. So after the thread computes it should post the result in the wParam.

In your most recent post you now have the result (newvalue) shown as a parameter, but you still don't use it! Instead, you are setting newvalue to something else, then displaying the something else. The task of the OnSendCalculatedValue function should be to display the newvalue that it receives from the thread.
Is there a possibility to calculate in the Thread procedure the
operation rand()*m_Slider.GetPos()
Yes. Whenever the slider changes you get OnHScroll. Have the main thread OnHScroll store the GetPos() in an int variable. The thread can read the int value without synchronization.
s***@mvps.org
2012-03-30 01:38:10 UTC
Permalink
In your original post you said you wanted to compute something in the thread 40 times and display the result in the GUI. So after the thread computes it should post the result in the wParam.

In your most recent post you now have the result (newvalue) shown as a parameter, but you still don't use it! Instead, you are setting newvalue to something else, then displaying the something else. The task of the OnSendCalculatedValue function should be to display the newvalue that it receives from the thread.
Is there a possibility to calculate in the Thread procedure the
operation rand()*m_Slider.GetPos()
Yes. Whenever the slider changes you get OnHScroll. Have the main thread OnHScroll store the GetPos() value in an int variable. The thread can read the int value without synchronization.
Mikel
2012-03-30 11:38:17 UTC
Permalink
Post by s***@mvps.org
In your original post you said you wanted to compute something in the thread 40 times and display the result in the GUI. So after the thread computes it should post the result in the wParam.
In your most recent post you now have the result (newvalue) shown as a parameter, but you still don't use it! Instead, you are setting newvalue to something else, then displaying the something else. The task of the OnSendCalculatedValue function should be to display the newvalue that it receives from the thread.
Is there a possibility to calculate in the Thread procedure the
operation rand()*m_Slider.GetPos()
Yes. Whenever the slider changes you get OnHScroll. Have the main thread OnHScroll store the GetPos() value in an int variable. The thread can read the int value without synchronization.
How can the thread read that int value? should the gui post a
PostThreadMessage to the thread with the value??
How about something like this:

class CWorkerThreadMgr
{
// Your stuff

protected:
int m_pos;

public:
void SetPos(int pos) { m_pos = pos; }
int GetPos() { return m_pos; }
}

void CStartCalculationDlg::OnHScroll(UINT nSBCode, UINT nPos,
CScrollBar* pScrollBar)
{
// Whatever you have now

m_WorkerThreadMgr.SetPos(m_Slider.GetPos());
}

static UINT WINAPI ThreadProc( LPVOID lptest )
{
CWorkerThreadMgr* pCalculateMgr = reinterpret_cast<
CWorkerThreadMgr*>( lptest);

for( UINT uCount = 0; uCount < 40; uCount++ ){
int value = pCalculateMgr->rand() *pCalculateMgr.GetPos();
PostMessage(pCalculateMgr-> m_hWnd, WM_SENDCALCULATED_VALUE,
value, 0);
}
}

LRESULT CStartCalculationDlg::OnSendCalculatedValue( WPARAM v,
LPARAM )
{
m_Calculation.Format(_T("%d"), (int)v);
SetDlgItemText(IDC_CALCULATION, m_Calculation);

return 1;
}
Lucress Carol
2012-03-30 12:14:43 UTC
Permalink
Post by Mikel
Post by s***@mvps.org
In your original post you said you wanted to compute something in the thread 40 times and display the result in the GUI. So after the thread computes it should post the result in the wParam.
In your most recent post you now have the result (newvalue) shown as a parameter, but you still don't use it! Instead, you are setting newvalue to something else, then displaying the something else. The task of the OnSendCalculatedValue function should be to display the newvalue that it receives from the thread.
Is there a possibility to calculate in the Thread procedure the
operation rand()*m_Slider.GetPos()
Yes. Whenever the slider changes you get OnHScroll. Have the main thread OnHScroll store the GetPos() value in an int variable. The thread can read the int value without synchronization.
How can the thread read that int value? should the gui post a
PostThreadMessage to the thread with the value??
class CWorkerThreadMgr
{
// Your stuff
    int m_pos;
    void SetPos(int pos) { m_pos = pos; }
    int GetPos() { return m_pos; }
}
void CStartCalculationDlg::OnHScroll(UINT nSBCode, UINT nPos,
CScrollBar* pScrollBar)
{
    // Whatever you have now
    m_WorkerThreadMgr.SetPos(m_Slider.GetPos());
}
static UINT WINAPI ThreadProc( LPVOID lptest )
{
    CWorkerThreadMgr* pCalculateMgr = reinterpret_cast<
CWorkerThreadMgr*>( lptest);
    for( UINT uCount = 0; uCount < 40; uCount++ ){
        int value = pCalculateMgr->rand() *pCalculateMgr.GetPos();
        PostMessage(pCalculateMgr-> m_hWnd, WM_SENDCALCULATED_VALUE,
value, 0);
    }
}
LRESULT CStartCalculationDlg::OnSendCalculatedValue( WPARAM v,
LPARAM )
{
      m_Calculation.Format(_T("%d"), (int)v);
      SetDlgItemText(IDC_CALCULATION, m_Calculation);
      return 1;
}- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
Thank you for the hint. I'm going to try it out ...
Lucress Carol
2012-03-30 11:10:50 UTC
Permalink
Post by s***@mvps.org
In your original post you said you wanted to compute something in the thread 40 times and display the result in the GUI. So after the thread computes it should post the result in the wParam.
In your most recent post you now have the result (newvalue) shown as a parameter, but you still don't use it! Instead, you are setting newvalue to something else, then displaying the something else. The task of the OnSendCalculatedValue function should be to display the newvalue that it receives from the thread.
Is there a possibility to calculate in the Thread procedure the
operation rand()*m_Slider.GetPos()
Yes. Whenever the slider changes you get OnHScroll. Have the main thread OnHScroll store the GetPos() value in an int variable. The thread can read the int value without synchronization.
How can the thread read that int value? should the gui post a
PostThreadMessage to the thread with the value??

Loading...