Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How MFC threads are created

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report