Skip to main content

Posts

Showing posts with the label blocking

How to stop a blocking thread from another function that start in other in MFC

ISSUE: In server client scenario it is mandatory to use thread where sever is listening on a particular port. Now in click of stop button ,how to stop this running thread. SOLUTION: #include <Windows.h> HANDLE m_Thread_handle; CWinThread *cTh; static UINT __cdecl StaticThreadFunc(LPVOID pParam); UINT ThreadFunc(); Step 1. Declared these on header file. Step 2. Starting the thread from a function or button click --- void CServerDlg::OnBnClickedStart() {     // TODO: Add your control notification handler code here     cTh = AfxBeginThread(     StaticThreadFunc,     this);     m_Thread_handle = cTh->m_hThread; } Step 3. Thread initialization function --  UINT __cdecl CServerDlg::StaticThreadFunc(LPVOID pParam) {     CServerDlg *pYourClass = reinterpret_cast<CServerDlg*>(pParam);     UINT retCode = pYourClass->ThreadFunc();   ...