您現(xiàn)在的位置是:首頁 >要聞 > 2023-08-20 21:42:15 來源:
vc多線程完整運行再回主線程(vc多線程)
大家好,我是小華,我來為大家解答以上問題。vc多線程完整運行再回主線程,vc多線程很多人還不知道,現(xiàn)在讓我們一起來看看吧!
1、在VC中可以調用MFC函數(shù)AfxBeginThread,創(chuàng)建線程。
2、線程做什么,怎么做就得看你自己的設計了。
3、貼一段MSDN里的代碼:Visual C++ Copy Code class CSockThread : public CWinThread{public: SOCKET m_hConnected;protected: CChatSocket m_sConnected; // remainder of class declaration omitted. Visual C++ Copy Code BOOL CSockThread::InitInstance(){ // Attach the socket object to the socket handle // in the context of this thread. m_sConnected.Attach(m_hConnected); m_hConnected = NULL; return TRUE;} Visual C++ Copy Code // This listening socket has been constructed// in the primary thread.void CListeningSocket::OnAccept(int nErrorCode){ UNREFERENCED_PARAMETER(nErrorCode); // This CSocket object is used just temporarily // to accept the incoming connection. CSocket sConnected; Accept(sConnected); // Start the other thread. CSockThread* pSockThread = (CSockThread*)AfxBeginThread( RUNTIME_CLASS(CSockThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); if (NULL != pSockThread) { // Detach the newly accepted socket and save // the SOCKET handle in our new thread object. // After detaching it, it should no longer be // used in the context of this thread. pSockThread->m_hConnected = sConnected.Detach(); pSockThread->ResumeThread(); }}。
本文到此講解完畢了,希望對大家有幫助。