Click to See Complete Forum and Search --> : access to UI controls from my thread.


Andrzej
March 24th, 2004, 02:38 PM
I have been trying to put my function in new thread, but I have still problem with set values of my UI controls. I know that I should use message to access UI elements from MFC thread. Colud anybody of us help me and show how to do it. Please let me explaim details ...

in CBaxView derrived from CFormView class I run my very big consumming CPU's time function (LoadBaxToTables()) it is memeber fun. of my CBaxHandling class derriverd from CObject.

void CBaxhView::OnFileLoaddb()
{
//create modeless dlg. to inform user about current step of running process
m_pDlgPerform = new CDlgPerform(this);
m_pDlgPerform->Create(IDD_DIALOG1_PERFORM_INFO);
m_pDlgPerform->ShowWindow(SW_SHOW);
// Load data to arrays, it is my luckless function
m_bax.LoadBaxToTables(); // CBaxHandling m_bax
....
}

Could you give me any suggestion or any example, how to run this function as new thread and simultaneously inform user via CDlgPerform about current step of function?
I am able to run this function as new thread but without possibilities informations for user. Whe I try to "touch" ui elements from my thread I receive errors.

Thanks in advance.

GCDEF
March 24th, 2004, 02:49 PM
First let me ask why you want another thread here?

Andrzej
March 24th, 2004, 03:00 PM
Originally posted by GCDEF
First let me ask why you want another thread here?

First:
the LoadBaxToTables() takes about 5 - 8 min. so the user should have possibility to stop it,
Second reason:
I'd like to put on Dialog somethink like "windmill" when process is running:

CString m_aSeqSigns[4] = { "|", "/", "---", "\\" };

SetTimer(.....)
...

void CDlgPerform::OnTimer(UINT nIDEvent)
{
CString strWind;
CWnd* pEditRun = GetDlgItem(IDC_EDIT_RUN);

strWind = m_aSeqSigns[(m_nCounter) % 4];
pEditRun->SetWindowText(strWind);
UpdateData(FALSE);
m_nCounter++;
}


if I don't use new thread the "windmill" start moving when the function is finisheg.

GCDEF
March 24th, 2004, 03:18 PM
In these situations I find it much easier just to use a PeekMessage and DispatchMessage in you time-consuming loop than to go messing around with threads.

Threads are useful if there is other work the user can be doing while your big process is running. If they just have to sit there anyway, a thread just adds unnecessary complexity.

Andreas Masur
March 24th, 2004, 04:56 PM
[Moved thread]

Andreas Masur
March 24th, 2004, 05:00 PM
Originally posted by GCDEF
In these situations I find it much easier just to use a PeekMessage and DispatchMessage in you time-consuming loop than to go messing around with threads.

Although I agree with you in general, that threads are being overused sometimes, it always kind of depends on the lengthy operation whether you can examine the message queue or not. If you do not have control over the source code of the function, then you are usually out of the game if you want to examine the message queue within.

Furthermore, examining the message queue might not always help you. If you cannot determine the processing speed of a block of code (or if it varies a lot during different runs), it is hard to find the correct places for adding the code for examing the message queue. In other words...if you have chosen a bad place...you still do not have a responsive GUI... :cool:

Andreas Masur
March 24th, 2004, 05:01 PM
Originally posted by Andrzej
I have been trying to put my function in new thread, but I have still problem with set values of my UI controls. I know that I should use message to access UI elements from MFC thread. Colud anybody of us help me and show how to do it. Please let me explaim details ...

Take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231250)...