Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
THREADSTRUCT* ts = (THREADSTRUCT*)param;



ts->_this->OnInitDialog();

Here OnInitDialog() in in main thread and i am trying to call this method from the child thread(worker thread) which i have created. when i am trying to do this i am getting a debug assertion failed error(Both in Debug mode and release mode).
** I am using Microsoft Visualc++ 6.0 IDE**
Posted

MFC objects are not thread safe. So you can't call MFC functions like OnInitDialog from another thread.

A usual solution to this problem is posting user defined messages from the worker thread to the other (main) thread.

As a starting point you may read Multithreading: Programming Tips[^] and follow the links. This has not changed much since VC 6. But you should think about using a more recent version.
 
Share this answer
 
This is less a problem of thread-safeness. MFC keeps a map of window handles to MFC objects, which is used to route an incoming windows message to the corresponding C++ class object. This map is thread-specific and can only be accessed from the thread in which the window was created. Hence, you may only deal with windows and controls from the thread that created them. A common technique is to use a single thread for all UI activities and worker threads for all non-UI activities.

That is also the reason why you must specify in MFC whether you want to create a UI or worker thread.
 
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