-
March 2nd, 2022, 01:27 AM
#1
worker thread access UI controls
I have not dealt with threads and got stock with writing an app for data acquisition experiments.
1) A thread launched by AfxThreadBegin, could it access Dialog controls and members for read only?. Or, even invoke buttons on dialog?.
2)Is below sudo code is right approach?. need the AfxThreadBegin wait till thread1 is done. not sure how to do this one.
Code:
//sudo code
void CMyDlg::callme()
{
///....
}
.... Thread1(...)
{
....
pCMy->callme();
}
void CMyDlg::Btn1()
{
.
AfxThreadBegin(...); //launch Thread1, how in here wait till Thread1() is finished?.
.
.
}
-
March 2nd, 2022, 03:00 AM
#2
Re: worker thread access UI controls
Both 1) and 2) are wrong.
A worker thread must not directly access the UI elements belonging to the windows of the main thread.
Instead a worker thread should PostMessage a user defined message with some data or/and info to the main thread to let it do the desired action.
See also this great Joe Newcomer's essay: http://www.flounder.com/mvp_tips.htm
Victor Nijegorodov
-
March 2nd, 2022, 10:43 AM
#3
Re: worker thread access UI controls
I know How to set a value to a UI control using postmessage but not reading it? How use the postmssage to read value from UI textbox?.
I have used sendmessage to invoke a button on UI. Is this safe?.
How about calling a non UI function from inside thread?. I do not need post/send message for that.?
-
March 2nd, 2022, 04:57 PM
#4
Re: worker thread access UI controls
Originally Posted by @EE@
How use the postmssage to read value from UI textbox?
It is up to main UI thread to supply the worker threads the needed data. However any work thread can query the currently needed data from the main thread using PostMessage.
Originally Posted by @EE@
I have used sendmessage to invoke a button on UI. Is this safe?
No. It usually causes a deadlock.
Originally Posted by @EE@
How about calling a non UI function from inside thread?. I do not need post/send message for that.?
Yes, it is possible, but be sure the thread calling this function won't modify some main thread values used for the main thread purposes!
Victor Nijegorodov
-
March 3rd, 2022, 03:33 AM
#5
Re: worker thread access UI controls
is it safe for the thread to call non UI member function of the Dialog that launched it?.
void CMyDlg::callme() //this is not part of UI. just a member of the Dialog
{
///....
}
.... Thread1(...)
{
....
pCMy->callme();
}
-
March 3rd, 2022, 04:31 AM
#6
Re: worker thread access UI controls
It depends upon what and how this callme() does with the dialog members.
Imagine the situation when you and some of your relatives go to the kitchen and eat a piece of cake. After the next attempt to eat a cake you'll find out that it has disappeared! But why????? Last time there was a big piece of cake but now you found nothing!
Understand?
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|