Update text box i a thread?

This is a discussion on Update text box i a thread? within the Windows Programming forums, part of the Platform Specific Boards category; I've got a nice little button: void CTest2Dlg::OnButton1() { MyThread newThread(&m_SerialText, &m_SerialTextControl, this); newThread.CreateNewThread(); } m_SerialText is a variable connected ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    Update text box i a thread?

    I've got a nice little button:
    void CTest2Dlg::OnButton1()
    {
    MyThread newThread(&m_SerialText, &m_SerialTextControl, this);

    newThread.CreateNewThread();
    }

    m_SerialText is a variable connected to an Edit Box.
    m_SerialTextControl is a control variable to the Edit box.


    In the CreateNewThread I do the following
    m_SerialText->Insert(m_SerialText->GetLength(), "nice thread");
    m_SerialText->Insert(m_SerialText->GetLength(), "\r\n");
    m_SerialTextControl->UpdateWindow();

    This does not work exacltly the way I want it to work. The edit box text is not updated until the thread is finished.
    I've tried to add m_SerialTextControl->UpdateData(FALSE); but then the program craches.

    How do you update a text in an edit box from a thread?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    From the new thread post the main thread a user message to tell it to update the m_SerialTextControl. The main thread needs to do to front end stuff.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    thank you! do you have an example I can look at?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Add a WindowProc() window procedure function using the ClassWizard then Post a user message to this dialog from the new thread, the main thread will receive the post in WindowProc() to perform the updates to the window.
    Code:
    //CTest2Dlg.h 
    
    #define WM_USER_MYUPDATE WM_USER +1
    
    class CTest2Dlg : public CDialog
    {
    public:
         ...
         ...
    // Overrides
         // ClassWizard generated virtual function overrides
         //{{AFX_VIRTUAL(CTest2Dlg)
         protected:
         virtual void DoDataExchange(CDataExchange* pDX);     // DDX/DDV support
         virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
         //}}AFX_VIRTUAL
         ...
         ...
    };
    
    //CTest2Dlg.cpp 
    LRESULT CTest2Dlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
    {
         //lParam = "nice thread"
    
         switch( message )
         {
         case WM_USER_MYUPDATE:
              m_SerialText->Insert(m_SerialText->GetLength(), (char *)lParam); 
              m_SerialText->Insert(m_SerialText->GetLength(), "\r\n");
              m_SerialTextControl->UpdateWindow();
              break;
         }
         return CDialog::WindowProc(message, wParam, lParam);
    }
    
    BOOL CTest2Dlg::OnInitDialog() 
    {
         CDialog::OnInitDialog();
    
         m_WorkThread = AfxBeginThread( ThreadProc , this);
    
         return TRUE;
    }
    
    UINT CTest2Dlg::ThreadProc( LPVOID pParam )
    {
         char strMsg[] = "nice thread"
         while( doing_something )
         {
              PostMessage(WM_USER_MYUPDATE, 0, (LPARAM )strMsg);
         }
    
         AfxEndThread(0);
    
         return 0;
    }
    Last edited by Scarlet7; 04-15-2003 at 08:08 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    Post

    Thank you! looks really nice!

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    That was quick! lol

    If the new thread is working hard then You might need to add a Sleep(10) to give the main thread a chance to update.
    Code:
         while( doing_something )
         {
              PostMessage(WM_USER_MYUPDATE, 0, (LPARAM )strMsg);
              Sleep(10);
         }

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    having trouble implementing this thread

    Scarlet

    I am using you example for a thread that drives a clock for my dialog box. When I try to build my MFC dialog application I get the error for the PostMessage function "Illegal call of non-static member function";

    I declared the member function ThreadProc as

    static UINT ThreadProc (LPVOID pParam)

    and followed the example posted here.

    I think the PostMessage function may need to be declared by me, but I thought it was a system defined function. Please straighten me out.

    Thanks in advance

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    >> edit box text is not updated until the thread is finished
    What are you doing in the main thread after start your thread? Are you allowing the message loop to run?

    You can do what you're trying to do, but I wouldn't pass around MFC control objects from thread to thread. Pass the HWND of the control you want your thread to access.
    Here is code for a thread that puts the number of seconds it has been running into an editbox (or any window that does something with WM_SETTEXT):
    Code:
    DWORD WINAPI threadproc(LPVOID data)
    {
        HWND editbox = reinterpret_cast<HWND>(data);
        DWORD n = 0;
        char temp[32] = "0";
    
        while (n < 60)
        {
            SendMessage(editbox, WM_SETTEXT, 0, (LPARAM)temp);
    
            Sleep(1000);
    
            n++;
            sprintf(temp, "%d", n);
        }//while
    
        return 0;
    }//threadproc
    >> I get the error for the PostMessage function...
    Put "::" infront of PostMessage().

    If you really want to "take the red pill" on MFC and multithreading, read this.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  3. Obtaining a value from a text box
    By thetinman in forum Windows Programming
    Replies: 1
    Last Post: 11-23-2006, 05:50 PM
  4. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  5. Adding text to a disabled edit box
    By osal in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21