Calling MFC Functions from Non-MFC Threads

When porting CVL applications to Visual C++ .NET, be aware that MFC will now assert if you make an MFC function call from a thread that is not an MFC thread. You can create MFC threads with the MFC function AfxBeginThread() or the CVL global function cfCreateThreadMFC(), explained below.

The implication for CVL is that when using Visual C++ .NET you cannot make any ccDisplayConsole function calls from threads that were created with cfCreateThread(). If you do, you may see asserts from deep in MFC with no clear indication of what the problem is or how to fix it. Moreover, it is possible to write code that is not thread safe and forget to call the cfThreadCleanup() function to clean up any memory after a thread is destroyed.

To solve these problems, the following changes have been made to the CVL thread creation API:

  • The new cfCreateThreadMFC() function allows creation of MFC worker threads using the CVL thread creation API. This makes it easier and less error-prone to update your applications when you move to Visual C++ .NET.
  • The cfCreateThread() function has been renamed to cfCreateThreadCVL(). The cfCreateThread() function is still available, but is deprecated. When compiling with Visual C++ .NET, any use of cfCreateThread() will cause a compiler warning (the new Visual C++ .NET deprecated function warning).

Always use cfCreateThreadMFC() to create MFC threads if your application is an MFC application and you are making calls to MFC functions (or ccDisplayConsole) from within the created thread.

See also Upgrading Visual C++ 6.0 Projects to .NET.