CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] GetDlgItemText and Multithreading

  1. #1
    Join Date
    Apr 2017
    Posts
    18

    [RESOLVED] GetDlgItemText and Multithreading

    So I have been trying to implement the concept of multithreading to an MFC application I am making. I used the method suggested here. It works fine but I am having an issue with using data given by the user while the thread is working.

    I'll explain.

    I am making a simple GUI to send and receive data over a serial port. So the data in IDC_SEND is user-input, and is then sent through the serial port. I am using the WINAPI definition of GetDlgItemText, but since the controlling function for AfxBeginThread is defined as a static function I cannot do this. So I tried ::GetDlgItemText, but that calls the CWnd definition which takes one or three or four(?) arguments.

    So ::GetDlgItemText(IDC_SEND, CString text) doesn't work. This problem continues for SetDlgItemText too.

    I have tried getting the data outside my controlling function, but since it is defined to return a UINT type, I cannot get the received data out.

    The relevant code

    Code:
    void CCommTest2Dlg::OnButton() 
    {   
    THREADSTRUCT *_param = new THREADSTRUCT;
    _param->_this = this;
    AfxBeginThread (StartThread, _param);
    }
    
    
    UINT CCommTest2Dlg::StartThread(LPVOID param)
    {
    THREADSTRUCT*    ts = (THREADSTRUCT*)param;
    AfxMessageBox ("Thread is started!");
    
    //Opened Serial Port
    
    //Writing data from Editbox
    CString text;
    
    ::GetDlgItemText(IDC_SEND,text);//********ERROR HERE!!
    serial.Write(text);
    
    //At Receiver port data is wriiten into CString a.
    
    CString a;
    
    ::SetDlgItemText( IDC_RECV, a);//Apparently this calls the SetDlgItemText from the CWnd class, and not the Windows API that takes more than one argument.
    AfxMessageBox ((LPCTSTR)a);//This works, but I need the data in the EditBox.
    
    //Closing Ports 
    
    delete ts; //Edit 1
    return 1;}
    A few definitions:

    Code:
    static UINT StartThread (LPVOID param);
    
    //structure for passing to the controlling function
    typedef struct THREADSTRUCT
           {
              CCommTest2Dlg*    _this;
    
            } THREADSTRUCT;
    
    UINT StartThread(void);
    Any thoughts?

    PS: Also Edit 1 at the end was added by me as I read that this implementation could result in memory leaks. Does it look like the addition might have fixed that?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,795

    Re: GetDlgItemText and Multithreading

    1. You have used a bad example.
    2. You must not touch any GUI object of the main thread from another thread.

    Please, check this great J.Newcomer's essay out: http://flounder.com/workerthreads.htm
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2017
    Posts
    18

    Re: GetDlgItemText and Multithreading

    Quote Originally Posted by VictorN View Post
    You have used a bad example.
    I have been getting the same response from a lot of people. Even the comment section of that example is filled with people saying it is a bad implementation.
    Quote Originally Posted by VictorN View Post
    You must not touch any GUI object of the main thread from another thread.
    Learning that the hard way.
    Quote Originally Posted by VictorN View Post
    check this great J.Newcomer's essay out
    I saw that article when I started the threading business in my application, thought it looked too complex. I will now sit and read through that thoroughly. Thank you for all your help.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,795

    Re: [RESOLVED] GetDlgItemText and Multithreading

    You are welcome!
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)