Get the App
SLTechnology News&Howtos  ›  Network Security  › 

How MFC threads are created

Shulou Source: shulou.com Published: 2022-06-01 07:12:35 09月15日 Update

MFC is divided into UI thread and worker thread. Generally, the current application is a main UI thread and N worker threads to complete the work. The main UI thread gets the information sent by the worker thread to refresh the interface.

However, because of work needs, MFC has projects to maintain, so learn how MFC creates UI threads and uses worker threads.

1. UI thread, inheriting the CWinThread class

1 class CAddDeviceApp: public CWinThread 2 {3 DECLARE_DYNCREATE (CAddDeviceApp) 4 protected: 5 CAddDeviceApp (); 6 public: 7 virtual BOOL InitInstance (); 8 virtual int ExitInstance (); 9 protected:10 virtual ~ CAddDeviceApp (); 11 DECLARE_MESSAGE_MAP () 12 13}

Thread InitInstance

1 BOOL CAddDeviceApp::InitInstance () 2 {3 CSecondThreadDlg dlg; 4 m_pMainWnd = & dlg; 5 INT_PTR nResponse = dlg.DoModal (); 6 if (nResponse = = IDOK) 7 {8} 9 else if (nResponse = = IDCANCEL) 10 {11} 12 return TRUE;13}

M_pMainWnd = & dlg; with this, the UI thread handles the message loop independently, and the launched UI thread DoModal dialog box does not block the main thread's dialog box. Start the UI thread: 1 CAddDeviceApp* pThread = (CAddDeviceApp*) AfxBeginThread (RUNTIME_CLASS (CAddDeviceApp))

2. Worker threads currently working in MFC are: MFC thread, C Run runtime thread, Boost thread.

1 boost::thread thrd (BoostThreadFunc); 2 3 _ beginthread (CRunThreadFunc,0,NULL); / / No 4 _ beginthreadex (NULL, 0, ThreadFunEx, NULL, 0 null); 5 pThread=AfxBeginThread (ThreadFunc,NULL,THREAD_PRIORITY_NORMAL)

BOOST threads can be created in many ways, such as function objects, Boost::bind member functions, and so on. Because the work does not have much time, it is not summarized.

Generally speaking, MFC is safer to use AfxBeginThread.

Tags: Threads work methods functions dialogs dialogs security no information can be passed through multiple objects applications members time messages interfaces categories programs projects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Redmi Xiaomi Shulou Tech Info