CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2019
    Posts
    72

    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?.
    
    .
    .
    
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  3. #3
    Join Date
    Aug 2019
    Posts
    72

    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.?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: worker thread access UI controls

    Quote Originally Posted by @EE@ View Post
    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.

    Quote Originally Posted by @EE@ View Post
    I have used sendmessage to invoke a button on UI. Is this safe?
    No. It usually causes a deadlock.

    Quote Originally Posted by @EE@ View Post
    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

  5. #5
    Join Date
    Aug 2019
    Posts
    72

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

    }

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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
  •  





Click Here to Expand Forum to Full Width

Featured