Re: Waiting in GUI till thread ends
Quote:
Originally Posted by zvivered
I solved the problem:
This works great (I Think).
You should rather have listened to our suggestions. An additional message pump as you coded it was the usual way to simulate some kind of "multithreading" back in the days of 16-bit Windows. Under Win32, this approach is discouraged (for various reasons). Why don't you simply add the "something else" to the end of your thread procedure? And if that "something else" is supposed to interact with the UI (which you didn't specify): Use a user-defined message and send it to your main thread (as suggested by torfil). And if your thread can't be started multiple times in parallel, but you need to prevent the button from being clicked while the thread executes (which, again, you didn't specify, but it has only been guessed): Disable the button as suggested by EoF.
Re: Waiting in GUI till thread ends
Quote:
Originally Posted by zvivered
I solved the problem:
while (GetMessage(&msg,NULL,NULL,NULL) && !ThreadEnd)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//Do someting ...
This works great (I Think).
If you're using multithreading, then this is a ridiculous solution. Why start the thread at all? Just do your "Acquire" function in the main thread and pump messages (as above) every once in a while during the acquisition.
The solution you have coded is like being given the keys to a Porsche, and then driving it onto a flatbed truck and driving the truck instead.
Mike
Re: Waiting in GUI till thread ends
Quote:
Originally Posted by gstercken
You should rather have listened to our suggestions. An additional message pump as you coded it was the usual way to simulate some kind of "multithreading" back in the days of 16-bit Windows. Under Win32, this approach is discouraged (for various reasons). Why don't you simply add the "something else" to the end of your thread procedure? And if that "something else" is supposed to interact with the UI (which you didn't specify): Use a user-defined message and send it to your main thread (as suggested by torfil). And if your thread can't be started multiple times in parallel, but you need to prevent the button from being clicked while the thread executes (which, again, you didn't specify, but it has only been guessed): Disable the button as suggested by EoF.
Thank you for your time. I learned a lot.
Re: Waiting in GUI till thread ends
Quote:
Originally Posted by MikeAThon
It should help: it's a great answer.
Just to be clear, your button click handler should do nothing except start the thread and then exit. The thread then posts the WM_MYTHREADENDED message when it's done, and your message handler for WM_MYTHREADENDED should then perform the steps that you originally wanted to do in your original button click handler (i.e., "the handler will do something else").
If you want to prevent the user from re-clicking the button while the thread is running (for example, maybe it would be a bad thing to try to acquire again while another acquisition is in-process), then you can disable the button in the button click handler and re-enable it in the handler for WM_MYTHREADENDED. Use EnableWindow(FALSE). For example, assuming that your dialog has a member variable of type "CButton" for the button, named m_ctlButtonAcquire:
Code:
void CThreadDlg::OnButton1()
{
CreateThread (0,1024*10,Acquire,this,0,&AcquireId);
m_ctlButtonAcquire.EnableWindow(FALSE);
}
LRESULT CThreadDlg::OnWmThreadEnded(LPARAM lParam, WPARAM wParam)
{
// TODO: Add the steps that you wanted to do in the
// original button click handler, i.e., "the handler will do
// something else"
m_ctlButtonAcquire.EnableWindow(TRUE);
return 0L;
}
Mike
Thank you for your help. I learned a lot.
Re: Waiting in GUI till thread ends