Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made one thread in my project and I use rich edit control to show data. When i run my appliaction and i try to show data in Rich edit control , It will hang my window.
So I want to make it thread-safe. What is the solution?

I have created thread using below code.

In h file
C++
static unsigned long __stdcall ReadThread1(void *ptr)
{
    ((CSimpleTestDlg*)ptr)->ReadThread();
    return 0;
}


In cpp file
C++
void CSimpleTestDlg::OnBnClickedOk()
{
   HANDLE rt;
   DWORD dwReadThread;
   rt = CreateThread(NULL , 0 , (LPTHREAD_START_ROUTINE)ReadThread1 , this , 0, &dwReadThread);
}

void CSimpleTestDlg::ReadThread()
{
    //code
}
Posted
Updated 5-May-11 21:49pm
v4
Comments
Richard MacCutchan 5-May-11 3:54am    
What is the solution?
Your code has a bug in it, or you are doing something wrong. Please do not assume we can guess what your program is doing. Try and be more specifiec about what happens in each thread, and how they interact. It sounds like you are doing some processing in the GUI thread that should be done in the background.
Olivier Levrey 6-May-11 3:50am    
The important thing is: what did you put inside ReadThread? This part is making your application hang.

1 solution

The rule is: never use UI functions or manipulate UI objects in another thread.

If you want a thread to update an UI object, you can for example post or send a message: the message will be handled by the UI thread.

For example, let's say you created a thread inside a CDialog-derived class:

In h file:
C++
class CYourDialog : public CDialog
{
    ...
    //the thread function
    static DWORD WINAPI YourThread(LPVOID param);
    ...
    //the message handler
    afx_msg LRESULT OnYourMessage(WPARAM wParam, LPARAM lParam);
};

In cpp file:
C++
//define a user message. Replace x by 0, 1, 2, ..., to define different message IDs.
#define WM_YOUR_MESSAGE (WM_USER + x)

BEGIN_MESSAGE_MAP(CYourDialog, CDialog)
    ....
    // map the message ID to the handler
    ON_MESSAGE(WM_YOUR_MESSAGE, OnYourMessage)
END_MESSAGE_MAP()

DWORD WINAPI CYourDialog::YourThread(LPVOID param)
{
    //when you create your thread
    //give this pointer as a parameter so you can get the pointer back here

    //get our dialog pointer
    CYourDialog* pDlg = (CYourDialog*)param;
    
    //SendMessage is synchronous: it will wait that the message is handled before returning
    //be carefull with that function, because it can easily cause deadlocks
    //if you block the main UI thread.
    pDlg->SendMessage(WM_YOUR_MESSAGE, param1, param2);
    //param1 and param2 can be pointers to structures: just cast them in LPARAM in WPARAM
    ...
    return 0;
}

LRESULT	CYourDialog::OnYourMessage(WPARAM wParam, LPARAM lParam)
{
    //here you can cast wParam and lParam back to their real types
    //your are in the UI thread, you can do whatever you want
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900